@nerdware/ddb-single-table
Version:
A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡
54 lines (53 loc) • 2.12 kB
JavaScript
import { isString, isBoolean, isBuffer, isDate, isArray, isPlainObject, } from "@nerdware/ts-type-safety-utils";
/**
* `number` type guard function
*
* ### DynamoDB Numbers
*
* - Can be positive, negative, or zero.
* - Can have up to 38 digits of precision (JS only supports up to 16 digits of precision).
* - Has a positive range of 1E-130 to 9.9999999999999999999999999999999999999E+125
* - Has a negative range of -9.9999999999999999999999999999999999999E+125 to -1E-130
* - Leading and trailing zeroes are trimmed.
*/
export const isNumber = (value) => {
// isFinite returns false for NaN, Infinity, -Infinity
return typeof value === "number" && Number.isFinite(value);
};
/**
* Type guard function for `type: "tuple"`
*
* > **Note:** This function does not check the types of values _within_ the provided tuple — that
* is accomplished by the `typeChecking` IO-Action which is applied recursively to nested values
* via `recursivelyApplyIOAction`.
*/
export const isTuple = (value, nestedSchema) => {
return isArray(value) && isArray(nestedSchema) && value.length === nestedSchema.length;
};
/** Type guard function for `type: "enum"` */
export const isEnumMember = (value, allowedValues) => {
return isString(value) && isArray(allowedValues) && allowedValues.includes(value);
};
/**
* Type guard/safety functions for each of the supported schema attribute `type` values.
*/
export const isType = Object.freeze({
/** Type guard function for `type: "string"` */
string: isString,
/** Type guard function for `type: "number"` */
number: isNumber,
/** Type guard function for `type: "boolean"` */
boolean: isBoolean,
/** Type guard function for `type: "Buffer"` */
Buffer: isBuffer,
/** Type guard function for `type: "Date"` */
Date: isDate,
/** Type guard function for `type: "array"` */
array: isArray,
/** Type guard function for `type: "map"` */
map: isPlainObject,
/** Type guard function for `type: "tuple"` */
tuple: isTuple,
/** Type guard function for `type: "enum"` */
enum: isEnumMember,
});