Skip to content

Commit

Permalink
✨ Add entries function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 8, 2021
1 parent 46c5492 commit e34bd23
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 4 deletions.
3 changes: 3 additions & 0 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const api: Api = {
},
dec: LAMBDAS,
endsWith: ALL_MODULES,
entries: {
lodash: 'entries'
},
F: LAMBDAS,
first: {
ramda: 'head',
Expand Down
113 changes: 111 additions & 2 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,115 @@ Deno.test('endsWith', () => {
---
### entries
:link: 
[keys](#keys) 
[values](#values) 
<p>
<span class="tag object mr-2">Object</span>
<span class="tag array mr-2">Array</span>
</p>
<span class="tag beta">beta</span>
Returns an array of key/values of the enumerable properties of an object.
{.desc}
::: warning
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.
:::
**Signature:**
```ts
entries$0: {
<T>(val: {
[key: string]: T;
} | ArrayLike<T>): [string, T][];
(val: Record<string, unknown>): [string, unknown][];
}
```
<details class="parameters-detail">
<summary>Parameters</summary>
| Parameter | Description |
| --------- | ----------- |
| `val` | `Object` that contains the properties and methods |
{.table}
<code class="returns">=></code> The result of `Object.entries(val)`
</details>
::: tip Remark
The order of the output array is not guaranteed to be consistent across different JS platforms.
:::
#### Example
```ts
entries({ a: 'b' }) // [['a', 'b']]
entries(['a', 'b', 'c']) // [['0', 'a'], ['1', 'b'], ['2', 'c']]
entries({}) // []
entries([]) // []
```
<details>
<summary>Tests</summary>
```ts
import { assertEquals } from '../deps.ts'
import { entries } from '../src/entries.ts'

Deno.test('entries', () => {
const table: [Record<PropertyKey, unknown> | ArrayLike<any>, any[]][] = [
[[], []],
[{}, []],
[[''], [['0', '']]],
[{ a: 1 }, [['a', 1]]],
[{ [Symbol('key')]: 1 }, []],
[{ [Symbol('key')]: 1, key: 'value' }, [['key', 'value']]],
[{ a: 'b' }, [['a', 'b']]],
[
{ a: 'b', c: 'd', e: 'f' },
[
['a', 'b'],
['c', 'd'],
['e', 'f']
]
],
[
{ a: 'b', 1: 'z', 0: 10, z: 1 },
[
['0', 10],
['1', 'z'],
['a', 'b'],
['z', 1]
]
],
[{ 1: 1 }, [['1', 1]]]
]
table.forEach(([val, expected]) => {
assertEquals(entries(val), expected, `entries(${val}) -> ${expected}`)
})
})
```
</details>
[View source on GitHub](https://github.com/TomokiMiyauci/fonction/blob/main/src/entries.ts)
---
### F
<small>Added from [1.1.0](./1.1.0/#f)</small>
Expand Down Expand Up @@ -3679,7 +3788,7 @@ Returns the names of the enumerable string properties and methods of an object.
**Signature:**
```ts
keys: <T extends PropertyKey>(val: Record<T, unknown>) => string[]
keys$0: <T extends PropertyKey>(val: Record<T, unknown>) => string[]
```
<details class="parameters-detail">
Expand Down Expand Up @@ -5906,7 +6015,7 @@ Returns an array of values of the enumerable properties of an object.
**Signature:**
```ts
values: <T extends unknown>(val: Record<PropertyKey, T> | ArrayLike<T>) => T[]
values$0: <T extends unknown>(val: Record<PropertyKey, T> | ArrayLike<T>) => T[]
```
<details class="parameters-detail">
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { dec } from './src/dec.ts'
export { defaultTo } from './src/defaultTo.ts'
export { divide } from './src/divide.ts'
export { endsWith } from './src/endsWith.ts'
export { entries } from './src/entries.ts'
export { F } from './src/F.ts'
export type { First } from './src/first.ts'
export { first } from './src/first.ts'
Expand Down
6 changes: 4 additions & 2 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
export { _ } from 'https://x.nest.land/arithmetic4@0.1.1/mod.ts'
export const NULL = null

const { hasOwnProperty } = Object.prototype
export { hasOwnProperty }
const { prototype, entries, keys, values } = Object
const { hasOwnProperty } = prototype

export { entries, hasOwnProperty, keys, values }
37 changes: 37 additions & 0 deletions src/entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
import { entries as _entries } from './constants/index.ts'
/**
* Returns an array of key/values of the enumerable properties of an object.
*
* @param val - `Object` that contains the properties and methods
* @returns The result of `Object.entries(val)`
*
* @remarks
* The order of the output array is not guaranteed to be consistent across different JS platforms.
*
* @example
* ```ts
* entries({ a: 'b' }) // [['a', 'b']]
* entries(['a', 'b', 'c']) // [['0', 'a'], ['1', 'b'], ['2', 'c']]
* entries({}) // []
* entries([]) // []
* ```
*
* @category `Object` `Array`
*
* @see Related to {@link keys} {@link values}
*
* @beta
*/
const entries: {
<T>(
val:
| {
[key: string]: T
}
| ArrayLike<T>
): [string, T][]
(val: Record<string, unknown>): [string, unknown][]
} = (val: any) => _entries(val)

export { entries }
36 changes: 36 additions & 0 deletions test/entries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
import { assertEquals } from '../deps.ts'
import { entries } from '../src/entries.ts'

Deno.test('entries', () => {
const table: [Record<PropertyKey, unknown> | ArrayLike<any>, any[]][] = [
[[], []],
[{}, []],
[[''], [['0', '']]],
[{ a: 1 }, [['a', 1]]],
[{ [Symbol('key')]: 1 }, []],
[{ [Symbol('key')]: 1, key: 'value' }, [['key', 'value']]],
[{ a: 'b' }, [['a', 'b']]],
[
{ a: 'b', c: 'd', e: 'f' },
[
['a', 'b'],
['c', 'd'],
['e', 'f']
]
],
[
{ a: 'b', 1: 'z', 0: 10, z: 1 },
[
['0', 10],
['1', 'z'],
['a', 'b'],
['z', 1]
]
],
[{ 1: 1 }, [['1', 1]]]
]
table.forEach(([val, expected]) => {
assertEquals(entries(val), expected, `entries(${val}) -> ${expected}`)
})
})

0 comments on commit e34bd23

Please sign in to comment.