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

Add correct handling of non string, number or symbol values to unique #343

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ const cluster = (list2, size = 2) => {
const unique = (array, toKey) => {
const valueMap = array.reduce((acc, item) => {
const key = toKey ? toKey(item) : item;
if (acc[key])
if (acc.get(key))
return acc;
acc[key] = item;
acc.set(key, item);
return acc;
}, {});
return Object.values(valueMap);
}, /* @__PURE__ */ new Map());
return Array.from(valueMap.values());
};
function* range(startOrLength, end, valueOrMapper = (i) => i, step = 1) {
const mapper = isFunction(valueOrMapper) ? valueOrMapper : () => valueOrMapper;
Expand Down
8 changes: 4 additions & 4 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ var radash = (function (exports) {
const unique = (array, toKey) => {
const valueMap = array.reduce((acc, item) => {
const key = toKey ? toKey(item) : item;
if (acc[key])
if (acc.get(key))
return acc;
acc[key] = item;
acc.set(key, item);
return acc;
}, {});
return Object.values(valueMap);
}, /* @__PURE__ */ new Map());
return Array.from(valueMap.values());
};
function* range(startOrLength, end, valueOrMapper = (i) => i, step = 1) {
const mapper = isFunction(valueOrMapper) ? valueOrMapper : () => valueOrMapper;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "11.0.0",
"version": "11.0.1",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
12 changes: 6 additions & 6 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,17 @@ export const cluster = <T>(list: readonly T[], size: number = 2): T[][] => {
* to convert each item in the list to a comparable identity
* value
*/
export const unique = <T, K extends string | number | symbol>(
export const unique = <T, K>(
array: readonly T[],
toKey?: (item: T) => K
): T[] => {
const valueMap = array.reduce((acc, item) => {
const key = toKey ? toKey(item) : (item as any as string | number | symbol)
if (acc[key]) return acc
acc[key] = item
const key = toKey ? toKey(item) : item
if (acc.get(key)) return acc
acc.set(key, item)
return acc
}, {} as Record<string | number | symbol, T>)
return Object.values(valueMap)
}, new Map())
return Array.from(valueMap.values())
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,27 @@ describe('array module', () => {
assert.equal(c.id, 'c')
assert.equal(c.word, 'yolo')
})
test('correctly handles non string, number or symbol values', () => {
const list = [
null,
null,
true,
true,
'true',
false,
{ id: 'a', word: 'hello' },
{ id: 'a', word: 'hello' }
]
const result = _.unique(list)
assert.deepEqual(result, [
null,
true,
'true',
false,
{ id: 'a', word: 'hello' },
{ id: 'a', word: 'hello' }
])
})
})

describe('range function', () => {
Expand Down
Loading