File size: 1,278 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

import CustomSocket from './CustomSocket';
import { type WorkerContext } from '../openapi/types';

class NumberSocket extends CustomSocket {
  override compatibleWith(socket: CustomSocket, noReverse: boolean): boolean {
    const cs: Partial<CustomSocket> = this;

    if (cs.type) {
      return ['integer', 'number', 'float'].includes(cs.type);
    }
    return socket instanceof CustomSocket;
  }

  async handleInput(ctx: WorkerContext, value: any): Promise<any | null> {
    if (Array.isArray(value)) {
      value = value[0]; // If array is passed in, just take the first element.
    }
    if (!value) {
      // e.g. undefined or [], which would otherwise be NaN.
      return 0;
    }
    if (value === 'inf') {
      return Infinity;
    }
    if (value === '-inf') {
      return -Infinity;
    }
    if (value === 'nan') {
      return NaN;
    }

    if (typeof value !== 'number') {
      return Number(value); // Use JavaScript's built-in conversion.
    }
    return value;
  }

  async handleOutput(ctx: WorkerContext, value: any): Promise<any | null> {
    return await this.handleInput(ctx, value); // Use the same logic for input and output.
  }
}

export default NumberSocket;