Hyperlinkv0.8.0-beta.28

SchemaAST

SchemaAST.Numberclasseffect/SchemaAST.ts:1354
Number

AST node matching any number value (including NaN, Infinity, -Infinity).

Details

Default JSON serialization:

  • Finite numbers are serialized as JSON numbers.
  • Infinity, -Infinity, and NaN are serialized as JSON strings.

If the node has an isFinite or isInt check, the string fallback is skipped since non-finite values cannot occur.

export class Number extends Base {
  readonly _tag = "Number"
  /** @internal */
  getParser() {
    return fromRefinement(this, Predicate.isNumber)
  }
  /** @internal */
  matchKey(s: string, options: ParseOptions): number | undefined {
    return this._match(isStringNumberRegExp, s, options)
  }
  /** @internal */
  matchPart(s: string, options: ParseOptions): number | undefined {
    return this._match(isStringFiniteRegExp, s, options)
  }
  private _match(regexp: RegExp, s: string, options: ParseOptions): number | undefined {
    return regexp.test(s)
      ? applyTemplateLiteralPartChecks(this, globalThis.Number(s), options)
      : undefined
  }
  /** @internal */
  toCodecJson(): AST {
    if (this.checks && (hasCheck(this.checks, "isFinite") || hasCheck(this.checks, "isInt"))) {
      return this
    }
    return replaceEncoding(this, [numberToJson])
  }
  /** @internal */
  toCodecStringTree(): AST {
    if (this.checks && (hasCheck(this.checks, "isFinite") || hasCheck(this.checks, "isInt"))) {
      return replaceEncoding(this, [finiteToString])
    }
    return replaceEncoding(this, [numberToString])
  }
  /** @internal */
  getExpected(): string {
    return "number"
  }
}
Referenced by 3 symbols