Function isSingle

Returns true if the data type is a "single" or "plain" value, in terms of NOT naturally/normally having compound or "Many" items (eg props, array items etc) inside it that we can iterate over.

Aka and similar to Scalar or Primitive, but there are distinctions - see this answer https://stackoverflow.com/a/6628566/799502 for context.

  • z.isSingle is similar to z.isPrimitive(), but :
  • z.isSingle also includes extras as "singles" (eg Date & RegExp), as they are not considered as having arbitrary props or contents (days months minutes etc in Date don't count as user-defined props).
  • z.isSingle also includes boxed values of primitives (eg. new Boolean(true)), unlike z.isPrimitive() which only allows for really primitive unboxed values.

Think of it as lodash's negated _.isObject(), which unfortunately can't be used as such cause it fails in few cases:

  z.isSingle(new String()) === true

but

  _.isObject(new String()) === true
  _.isObject(new Date()) === true
  _.isObject(new RegExp()) === true

For reference, isSingle(value) correctly recognises all of these value types:

 'string',
 'number',
 'boolean',
 'undefined',
 'null',
 'symbol',
 'bigint',

 // extra to `isPrimitive()`:
 'Date',
 'RegExp',
 'function',
 'Promise'
 'NaN'
 'Boolean',
 'String',
 'Number'
 'Error'

 // Special cases, as they might have many items internally, but practically they are not iterable, hence not "many" nor "single":
 WeakSet
 WeakMap

irrespective of how these values are created (literal/primitive or new Xxx).

  • z.isMany() is the opposite of isSingle
  • z.isPrimitive() for the most basic single types
  • SINGLE_NAMES for the string list of types that are considered "Single" types.
  • Single the types that are considered "Single".
  • isSingleType to check if a type (not a value) is a "Single" type.