How to use kind-of - 9 common examples

To help you get started, we’ve selected a few kind-of examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ianstormtaylor / superstruct / src / structs / record.ts View on Github external
Struct.check = (
    value: any,
    branch: Branch,
    path: Path
  ): [Failure[]?, any?] => {
    // Record structs have a special default handling behavior, where the defaults
    // are for the entries themselves, not for the entire value. So we can't use
    // JavaScript's built-in default handling here.
    const defs = Struct.default()
    value = defs ? { ...defs, ...value } : value

    if (kindOf(value) !== 'object') {
      return [[Struct.fail({ value, branch, path })]]
    }

    const result = {}
    const failures: Failure[] = []

    for (let k in value) {
      const v = value[k]
      const p = path.concat(k)
      const b = branch.concat(v)
      const [kfs, kr] = Key.check(k, b, p)

      if (kfs) {
        failures.push(...kfs)
      } else {
        const [vfs, vr] = Value.check(v, b, p)
github reixs / reixs / src / core / constructor / scheduler.js View on Github external
overtime(time) {
        if (kindOf(time) === 'number' || time === null) {
            this._config.overtime = time
        } else {
            throw new Error('Invalid type')
        }
        return this
    }
github ianstormtaylor / superstruct / src / kinds.js View on Github external
function inter(schema, defaults, options) {
  if (kindOf(schema) !== 'object') {
    if (process.env.NODE_ENV !== 'production') {
      throw new Error(
        `Interface structs must be defined as an object, but you passed: ${schema}`
      )
    } else {
      throw new Error(`Invalid schema: ${schema}`)
    }
  }

  const obj = scalar('object', undefined, options)
  const ks = []
  const properties = {}

  for (const key in schema) {
    ks.push(key)
    const s = schema[key]
github ianstormtaylor / superstruct / src / structs / partial.ts View on Github external
Struct.check = (
    value: any = Struct.default(),
    branch: Branch,
    path: Path
  ): [Failure[]?, any?] => {
    const d = Struct.default()

    if (value === undefined) {
      value = d
    }

    if (kindOf(value) !== 'object') {
      return [[Struct.fail({ value, branch, path })]]
    }

    const result = {}
    const failures: Failure[] = []

    for (const k of value) {
      let v = value[k]
      const p = path.concat(k)
      const b = branch.concat(v)
      const Prop = Props[k]

      if (v === undefined && d != null && k in d) {
        v = typeof d[k] === 'function' ? d[k](value, branch, path) : d[k]
      }
github ianstormtaylor / superstruct / src / kinds.js View on Github external
function intersection(schema, defaults, options) {
  if (kindOf(schema) !== 'array') {
    if (process.env.NODE_ENV !== 'production') {
      throw new Error(
        `Intersection structs must be defined as an array, but you passed: ${schema}`
      )
    } else {
      throw new Error(`Invalid schema: ${schema}`)
    }
  }

  const types = schema.map(s => any(s, undefined, options))
  const name = 'intersection'
  const type = types.map(t => t.type).join(' & ')
  const validate = (value = resolveDefaults(defaults)) => {
    let v = value

    for (const t of types) {
github ianstormtaylor / superstruct / src / kinds.js View on Github external
function func(schema, defaults, options) {
  if (kindOf(schema) !== 'function') {
    if (process.env.NODE_ENV !== 'production') {
      throw new Error(
        `Function structs must be defined as a function, but you passed: ${schema}`
      )
    } else {
      throw new Error(`Invalid schema: ${schema}`)
    }
  }

  const name = 'function'
  const type = ''
  const validate = (value = resolveDefaults(defaults), data) => {
    const result = schema(value, data)
    let failure = { path: [], reason: null }
    let isValid
github terascope / teraslice / packages / utils / src / utils.ts View on Github external
export function getTypeOf(val: any): string {
    if (val === undefined) return 'undefined';
    if (val === null) return 'null';

    if (typeof val === 'object') {
        if (val.__isDataEntity) return 'DataEntity';
        if (val.constructor && val.constructor.name) {
            return val.constructor.name;
        }
        if (val.prototype && val.prototype.name) {
            return val.prototype.name;
        }
    }

    const kind = kindOf(val);
    return firstToUpper(kind);
}
github ianstormtaylor / superstruct / src / kinds.js View on Github external
function scalar(schema, defaults, options) {
  if (kindOf(schema) !== 'string') {
    if (process.env.NODE_ENV !== 'production') {
      throw new Error(
        `Scalar structs must be defined as a string, but you passed: ${schema}`
      )
    } else {
      throw new Error(`Invalid schema: ${schema}`)
    }
  }

  const { types } = options
  const fn = types[schema]

  if (kindOf(fn) !== 'function') {
    if (process.env.NODE_ENV !== 'production') {
      throw new Error(
        `No struct validator function found for type "${schema}".`
      )
    } else {
      throw new Error(`Invalid type: ${schema}`)
    }
  }

  const kind = func(fn, defaults, options)
  const name = 'scalar'
  const type = schema
  const validate = value => {
    const [error, result] = kind.validate(value)

    if (error) {
github ianstormtaylor / superstruct / src / kinds.js View on Github external
function any(schema, defaults, options) {
  if (isStruct(schema)) {
    return schema[KIND]
  }

  if (schema instanceof Kind) {
    return schema
  }

  switch (kindOf(schema)) {
    case 'array': {
      return schema.length > 1
        ? tuple(schema, defaults, options)
        : list(schema, defaults, options)
    }

    case 'function': {
      return func(schema, defaults, options)
    }

    case 'object': {
      return object(schema, defaults, options)
    }

    case 'string': {
      let required = true

kind-of

Get the native type of a value.

MIT
Latest version published 4 years ago

Package Health Score

74 / 100
Full package analysis

Popular kind-of functions