Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map entries with filtering #367

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"autoHide.autoHidePanel": false
}
5 changes: 3 additions & 2 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,9 @@ const mapEntries = (obj, toEntry) => {
if (!obj)
return {};
return Object.entries(obj).reduce((acc, [key, value]) => {
const [newKey, newValue] = toEntry(key, value);
acc[newKey] = newValue;
const alteredEntry = toEntry(key, value);
if (alteredEntry !== void 0)
acc[alteredEntry[0]] = alteredEntry[1];
return acc;
}, {});
};
Expand Down
5 changes: 3 additions & 2 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,9 @@ var radash = (function (exports) {
if (!obj)
return {};
return Object.entries(obj).reduce((acc, [key, value]) => {
const [newKey, newValue] = toEntry(key, value);
acc[newKey] = newValue;
const alteredEntry = toEntry(key, value);
if (alteredEntry !== void 0)
acc[alteredEntry[0]] = alteredEntry[1];
return acc;
}, {});
};
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions docs/object/map-entries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ group: Object
Iterates the entries of an object, calling the given `toEntry` callback function
to generate new entries. It's a `_.mapValues` and `_.mapKeys`
in one. The `toEntry` callback function should return an array with
two items `[key, value]` (a.k.a the new entry).
two items `[key, value]` (a.k.a the new entry). If the function returns
`undefined`, the entry will be ignored

```ts
import { mapEntries } from 'radash'
Expand All @@ -21,5 +22,6 @@ const ra = {
culture: 'egypt'
}

mapEntries(ra, (key, value) => [key.toUpperCase(), `${value}`]) // => { NAME: 'Ra', POWER: 'sun', RANK: '100', CULTURE: 'egypt' }
mapEntries(ra, (key, value) => key === 'culture' ? undefined : [key.toUpperCase(), `${value}`])
// => { NAME: 'Ra', POWER: 'sun', RANK: '100' }
```
6 changes: 3 additions & 3 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ export const mapEntries = <
TNewValue
>(
obj: Record<TKey, TValue>,
toEntry: (key: TKey, value: TValue) => [TNewKey, TNewValue]
toEntry: (key: TKey, value: TValue) => [TNewKey, TNewValue] | undefined
): Record<TNewKey, TNewValue> => {
if (!obj) return {} as Record<TNewKey, TNewValue>
return Object.entries(obj).reduce((acc, [key, value]) => {
const [newKey, newValue] = toEntry(key as TKey, value as TValue)
acc[newKey] = newValue
const alteredEntry = toEntry(key as TKey, value as TValue)
if (alteredEntry !== undefined) acc[alteredEntry[0]] = alteredEntry[1]
return acc
}, {} as Record<TNewKey, TNewValue>)
}
Expand Down
9 changes: 9 additions & 0 deletions src/tests/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ describe('object module', () => {
assert.equal(result.fey, 'USER')
assert.equal(result.bray, 'GUEST')
})
test('filtering', () => {
const result = _.mapEntries(peopleByRole, (key, value) =>
key === 'guest' ? undefined : [value, key.toUpperCase()]
)
assert.equal(Object.keys(result).length, 2)
assert.equal(result.jay, 'ADMIN')
assert.equal(result.fey, 'USER')
assert.equal(result.bray, undefined)
})
})

describe('invert function', () => {
Expand Down