Skip to content

Commit

Permalink
✨ Add keys and values functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 28, 2021
1 parent bb0315d commit a8165cd
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
| [isSymbol](./issymbol/) | Whatever argument is type of <code>symbol</code> or not. |
| [isUndefined](./isundefined/) | Whatever argument is type of <code>undefined</code> or not. |
| [K](./k/) | K combinator. Returns a function that always returns the given value. |
| [keys](./keys/) | <b><i>(BETA)</i></b> 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 |
Expand All @@ -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/) | <b><i>(BETA)</i></b> 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
Expand Down
31 changes: 31 additions & 0 deletions docs/api/keys/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->



## 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.

<b>Signature:</b>

```typescript
keys: <T extends PropertyKey>(val: Record<T, unknown>) => 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']

```

42 changes: 42 additions & 0 deletions docs/api/values/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->



## 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.

<b>Signature:</b>

```typescript
values: <T extends unknown>(val: Record<PropertyKey, T> | ArrayLike<T>) => 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']

```

2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
22 changes: 22 additions & 0 deletions src/keys.ts
Original file line number Diff line number Diff line change
@@ -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 = <T extends PropertyKey>(val: Record<T, unknown>): string[] =>
Object.keys(val)

export { keys }
31 changes: 31 additions & 0 deletions src/values.ts
Original file line number Diff line number Diff line change
@@ -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 = <T extends unknown>(
val: Record<PropertyKey, T> | ArrayLike<T>
): T[] => Object.values(val)

export { values }
21 changes: 21 additions & 0 deletions test/keys.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { keys } from '@/keys'

describe('keys', () => {
const table: [Record<PropertyKey, unknown>, 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)
})
})
30 changes: 30 additions & 0 deletions test/values.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { values } from '@/values'

describe('values', () => {
const table: [Record<PropertyKey, unknown> | ArrayLike<any>, 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)
})
})

0 comments on commit a8165cd

Please sign in to comment.