@karmaniverous/entity-tools
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -80,4 +80,16 @@ 'use strict'; | ||
}, | ||
timestamp: { | ||
encode: (value) => { | ||
if (!radash.isInt(value) || value < 0 || value > 9999999999999) | ||
throw new Error('invalid timestamp'); | ||
return value.toString().padStart(13, '0'); | ||
}, | ||
decode: (value) => { | ||
if (!radash.isString(value) || !/^[0-9]{13}$/.test(value)) | ||
throw new Error('invalid encoded timestamp'); | ||
return Number(value); | ||
}, | ||
}, | ||
}; | ||
exports.defaultTranscodes = defaultTranscodes; |
@@ -47,10 +47,14 @@ /** | ||
string: string; | ||
/** | ||
* Supports transcoding of UNIX timestamp values. See {@link defaultTranscodes | `defaultTranscodes`} for implementation details. | ||
*/ | ||
timestamp: number; | ||
} | ||
/** | ||
* Strips the generic `[x: string]: unknown` property from `Record<string, unknown>` type. | ||
* Strips the unspecified properties like `[x: string]: unknown` from `Record` types. | ||
* | ||
* @typeParam T - The `Record<string, unknown>` type. | ||
* @typeParam T - The `Record` type. | ||
* | ||
* @returns The `Record<string, unknown>` type without the generic property. | ||
* @returns The `Record` type with only specified properties. | ||
* | ||
@@ -60,3 +64,3 @@ * @category Utilities | ||
type Exactify<T extends Record<string, unknown>> = { | ||
[P in keyof T as string extends P ? never : P]: T[P]; | ||
[P in keyof T as string extends P ? never : number extends P ? never : symbol extends P ? never : P]: T[P]; | ||
}; | ||
@@ -76,3 +80,3 @@ | ||
* | ||
* Encoded strings should be articulated such that they sort alphanumerically is the same order as the mapped type. Numerical values should therefore be encoded at a foxed length. | ||
* Encoded strings should be articulated such that they sort alphanumerically in the same order as the mapped type. Numerical values should therefore be encoded at a foxed length. | ||
* | ||
@@ -79,0 +83,0 @@ * @example |
@@ -78,4 +78,16 @@ import { isString, isNumber, isInt } from 'radash'; | ||
}, | ||
timestamp: { | ||
encode: (value) => { | ||
if (!isInt(value) || value < 0 || value > 9999999999999) | ||
throw new Error('invalid timestamp'); | ||
return value.toString().padStart(13, '0'); | ||
}, | ||
decode: (value) => { | ||
if (!isString(value) || !/^[0-9]{13}$/.test(value)) | ||
throw new Error('invalid encoded timestamp'); | ||
return Number(value); | ||
}, | ||
}, | ||
}; | ||
export { defaultTranscodes }; |
@@ -130,3 +130,3 @@ { | ||
"types": "dist/index.d.ts", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"dependencies": { | ||
@@ -133,0 +133,0 @@ "radash": "^12.1.0" |
24206
541