diff --git a/src/predicate/index.ts b/src/predicate/index.ts index 57b972c..b7b66d4 100644 --- a/src/predicate/index.ts +++ b/src/predicate/index.ts @@ -9,6 +9,7 @@ export * from './isBigIntArray.js'; export * from './isBinaryNumberString.js'; export * from './isBoolean.js'; export * from './isBooleanArray.js'; +export * from './isDateString.js'; export * from './isDigitsString.js'; export * from './isFiniteNumber.js'; export * from './isFunction.js'; @@ -28,6 +29,7 @@ export * from './isNumericString.js'; export * from './isNumericValue.js'; export * from './isNumericValueArray.js'; export * from './isObject.js'; +export * from './isObjectLiteral.js'; export * from './isObjectWith.js'; export * from './isOctalNumberString.js'; export * from './isOptionalArray.js'; diff --git a/src/predicate/isDateString.ts b/src/predicate/isDateString.ts new file mode 100644 index 0000000..3529b1d --- /dev/null +++ b/src/predicate/isDateString.ts @@ -0,0 +1,7 @@ +import type { TypePredicateFn } from '../types/functions.js'; + +/** + * Determine if `input` is a valid date string. + */ +export const isDateString: TypePredicateFn = (input: unknown): input is string => + typeof input === 'string' && !Number.isNaN(new Date(input).getTime()); diff --git a/src/predicate/isObjectLiteral.ts b/src/predicate/isObjectLiteral.ts new file mode 100644 index 0000000..caa852e --- /dev/null +++ b/src/predicate/isObjectLiteral.ts @@ -0,0 +1,7 @@ +import type { UnknownRecord } from '../types/records.js'; + +/** + * Determine if `input` is an object literal. + */ +export const isObjectLiteral = (input: unknown): input is UnknownRecord => + input !== null && typeof input === 'object' && Object.getPrototypeOf(input) === Object.prototype; diff --git a/test/predicate/isDateString.test.ts b/test/predicate/isDateString.test.ts new file mode 100644 index 0000000..d3582db --- /dev/null +++ b/test/predicate/isDateString.test.ts @@ -0,0 +1,19 @@ +import { describe, it, expect } from 'vitest'; + +import { isDateString } from '../../src/predicate/isDateString.js'; + +describe('isDateString()', () => { + it.each([ + new Date().toISOString(), + new Date().toLocaleDateString('en-US'), + new Date().toLocaleString(), + new Date().toUTCString(), + new Date().toString(), + ])('Returns true for valid input: %j', (input) => { + expect(isDateString(input)).toBeTruthy(); + }); + + it.each([Date.now(), 'bad input', true, null, {}, undefined])('Returns false for invalid input: %j', (input) => { + expect(isDateString(input)).toBeFalsy(); + }); +}); diff --git a/test/predicate/isObjectLiteral.test.ts b/test/predicate/isObjectLiteral.test.ts new file mode 100644 index 0000000..46ba6ee --- /dev/null +++ b/test/predicate/isObjectLiteral.test.ts @@ -0,0 +1,20 @@ +import { describe, it, expect } from 'vitest'; + +import { isObjectLiteral } from '../../src/predicate/isObjectLiteral.js'; + +describe('isObjectLiteral()', () => { + it('Returns true for valid input', () => { + expect( + isObjectLiteral({ + test: true, + }), + ).toBeTruthy(); + }); + + it.each([Object.create(null), new Date(), globalThis, 1, 'a', true, null, undefined])( + 'Returns false for invalid input: %j', + (input) => { + expect(isObjectLiteral(input)).toBeFalsy(); + }, + ); +});