From a8165cd8827ef58335b16b2ef915d8b37fc07e1d Mon Sep 17 00:00:00 2001 From: TomokiMiyauci Date: Wed, 28 Apr 2021 21:38:57 +0900 Subject: [PATCH] :sparkles: Add keys and values functions --- docs/api/index.md | 2 ++ docs/api/keys/index.md | 31 +++++++++++++++++++++++++++++ docs/api/values/index.md | 42 ++++++++++++++++++++++++++++++++++++++++ mod.ts | 2 ++ src/keys.ts | 22 +++++++++++++++++++++ src/values.ts | 31 +++++++++++++++++++++++++++++ test/keys.spec.ts | 21 ++++++++++++++++++++ test/values.spec.ts | 30 ++++++++++++++++++++++++++++ 8 files changed, 181 insertions(+) create mode 100644 docs/api/keys/index.md create mode 100644 docs/api/values/index.md create mode 100644 src/keys.ts create mode 100644 src/values.ts create mode 100644 test/keys.spec.ts create mode 100644 test/values.spec.ts diff --git a/docs/api/index.md b/docs/api/index.md index 9ba51bc3..ee09bc98 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -36,6 +36,7 @@ | [isSymbol](./issymbol/) | Whatever argument is type of symbol or not. | | [isUndefined](./isundefined/) | Whatever argument is type of undefined or not. | | [K](./k/) | K combinator. Returns a function that always returns the given value. | +| [keys](./keys/) | (BETA) Returns the names of the enumerable string properties and methods of an object. | | [last](./last/) | Returns the last element of the given list or string. | | [length](./length/) | Returns the number of elements in the array or string length. | | [lowerCase](./lowercase/) | Return lowercase string | @@ -53,6 +54,7 @@ | [tail](./tail/) | Returns all but the first element of the given list or string. | | [trim](./trim/) | Removes (strips) whitespace from both ends of the string. | | [upperCase](./uppercase/) | Return uppercase string | +| [values](./values/) | (BETA) Returns an array of values of the enumerable properties of an object. | | [xor](./xor/) | Returns true if one of the arguments is truthy and the other is falsy; otherwise false. | ## Type Aliases diff --git a/docs/api/keys/index.md b/docs/api/keys/index.md new file mode 100644 index 00000000..b578dfb9 --- /dev/null +++ b/docs/api/keys/index.md @@ -0,0 +1,31 @@ + + + + +## keys variable + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Returns the names of the enumerable string properties and methods of an object. + +Signature: + +```typescript +keys: (val: Record) => string[] +``` + +## Remarks + +The order of the output array is not guaranteed to be consistent across different JS platforms. + +## Example + + +```ts +keys({}) // [] +keys({ 'a': 'b' }) // ['a'] +keys({ 0: 'hello', 1: 'world' }) // ['0', '1'] + +``` + diff --git a/docs/api/values/index.md b/docs/api/values/index.md new file mode 100644 index 00000000..a9ebfe5d --- /dev/null +++ b/docs/api/values/index.md @@ -0,0 +1,42 @@ + + + + +## values variable + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Returns an array of values of the enumerable properties of an object. + +Signature: + +```typescript +values: (val: Record | ArrayLike) => T[] +``` + +## Remarks + +The order of the output array is not guaranteed to be consistent across different platforms. + +## Example 1 + + +```ts +// Object +values({}) // [] +values({ 'a': 'b' }) // ['b'] +values({ 0: 'hello', 1: 'world' }) // ['hello', 'world'] + +``` + +## Example 2 + + +```ts +// Array +values([]) // [] +values(['hello', 'world']) // ['hello', 'world'] + +``` + diff --git a/mod.ts b/mod.ts index 56b6788a..3dababdf 100644 --- a/mod.ts +++ b/mod.ts @@ -27,6 +27,7 @@ export { isString } from './src/isString.ts' export { isSymbol } from './src/isSymbol.ts' export { isUndefined } from './src/isUndefined.ts' export { K } from './src/K.ts' +export { keys } from './src/keys.ts' export { last } from './src/last.ts' export { length } from './src/length.ts' export { lowerCase } from './src/lowerCase.ts' @@ -45,4 +46,5 @@ export { tail } from './src/tail.ts' export { trim } from './src/trim.ts' export type { AnyFn, Empty, Ord, Primitive } from './src/types.ts' export { upperCase } from './src/upperCase.ts' +export { values } from './src/values.ts' export { xor } from './src/xor.ts' diff --git a/src/keys.ts b/src/keys.ts new file mode 100644 index 00000000..feddbc81 --- /dev/null +++ b/src/keys.ts @@ -0,0 +1,22 @@ +/** + * Returns the names of the enumerable string properties and methods of an object. + * + * @param val - `Object` that contains the properties and methods + * @returns The result of `Object.keys(val)` + * + * @remarks + * The order of the output array is not guaranteed to be consistent across different JS platforms. + * + * @example + * ```ts + * keys({}) // [] + * keys({ 'a': 'b' }) // ['a'] + * keys({ 0: 'hello', 1: 'world' }) // ['0', '1'] + * ``` + * + * @beta + */ +const keys = (val: Record): string[] => + Object.keys(val) + +export { keys } diff --git a/src/values.ts b/src/values.ts new file mode 100644 index 00000000..f1b7504b --- /dev/null +++ b/src/values.ts @@ -0,0 +1,31 @@ +/** + * Returns an array of values of the enumerable properties of an object. + * + * @param val - `Object` that contains the properties and methods + * @returns The result of `Object.values(val)` + * + * @remarks + * The order of the output array is not guaranteed to be consistent across different platforms. + * + * @example + * ```ts + * // Object + * values({}) // [] + * values({ 'a': 'b' }) // ['b'] + * values({ 0: 'hello', 1: 'world' }) // ['hello', 'world'] + * ``` + * + * @example + * ```ts + * // Array + * values([]) // [] + * values(['hello', 'world']) // ['hello', 'world'] + * ``` + * + * @beta + */ +const values = ( + val: Record | ArrayLike +): T[] => Object.values(val) + +export { values } diff --git a/test/keys.spec.ts b/test/keys.spec.ts new file mode 100644 index 00000000..46554432 --- /dev/null +++ b/test/keys.spec.ts @@ -0,0 +1,21 @@ +import { keys } from '@/keys' + +describe('keys', () => { + const table: [Record, string[]][] = [ + [{}, []], + [{ '': '' }, ['']], + [{ '': undefined }, ['']], + [{ '': null }, ['']], + [{ '': 0 }, ['']], + [{ a: 0 }, ['a']], + [{ a: 0, b: 1 }, ['a', 'b']], + [{ a: 0, b: 1, c: 2 }, ['a', 'b', 'c']], + [{ 0: '' }, ['0']], + [{ 0: '', 1: 'a' }, ['0', '1']], + [{ '': {}, 1: [] }, ['1', '']] + ] + + it.each(table)('keys(%s) -> %s', (val, expected) => { + expect(keys(val)).toEqual(expected) + }) +}) diff --git a/test/values.spec.ts b/test/values.spec.ts new file mode 100644 index 00000000..4d913596 --- /dev/null +++ b/test/values.spec.ts @@ -0,0 +1,30 @@ +import { values } from '@/values' + +describe('values', () => { + const table: [Record | ArrayLike, any[]][] = [ + [{}, []], + [{ '': '' }, ['']], + [{ '': undefined }, [undefined]], + [{ '': null }, [null]], + [{ '': 0 }, [0]], + [{ a: 0 }, [0]], + [{ a: 0, b: 1 }, [0, 1]], + [{ a: 0, b: 1, c: 2 }, [0, 1, 2]], + [{ 0: '' }, ['']], + [{ 0: '', 1: 'a' }, ['', 'a']], + [{ '': {}, 1: [] }, [[], {}]], + [[], []], + [[''], ['']], + [[0], [0]], + [[null], [null]], + [[undefined], [undefined]], + [ + ['hello', 'world'], + ['hello', 'world'] + ] + ] + + it.each(table)('values(%s) -> %s', (val, expected) => { + expect(values(val)).toEqual(expected) + }) +})