Skip to content

Commit

Permalink
[fix] Nullish case in runtime #6
Browse files Browse the repository at this point in the history
[optimize] add Mutation notice in ReadMe
  • Loading branch information
TechQuery committed Jul 14, 2020
1 parent 83d4837 commit dfab433
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
package-lock.json
dist/
.vscode/
5 changes: 5 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ While `Array.prototype.unique()` invoked with:

3. one **function** parameter, it'll call this function for each array element, and then deduplicates the origin array based on these _returned values_.

Notice:

- the **Returned value** is a new array, no mutation happens in the original array
- **Empty/nullish items** are treated as nullish values

## Typical cases

```JavaScript
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "array-unique-proposal",
"version": "0.2.0",
"version": "0.2.1",
"description": "ECMAScript proposal for Deduplicating method of Array",
"keywords": [
"JavaScript",
Expand Down Expand Up @@ -29,13 +29,13 @@
"prepublishOnly": "npm test && npm run build"
},
"devDependencies": {
"@types/jest": "^26.0.0",
"@types/jest": "^26.0.4",
"husky": "^4.2.5",
"jest": "^26.0.1",
"lint-staged": "^10.2.9",
"jest": "^26.1.0",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"ts-jest": "^26.1.0",
"typescript": "^3.9.5"
"ts-jest": "^26.1.2",
"typescript": "^3.9.6"
},
"prettier": {
"singleQuote": true,
Expand Down
8 changes: 8 additions & 0 deletions polyfill/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ describe('Array.prototype.unique()', () => {
origin
);
});

it('should treat an empty/nullish item as a nullish value', () => {
expect([1, , 2, undefined, null, 1].unique()).toEqual([1, , 2, null]);

expect(
[{ id: 1 }, , { id: 2 }, undefined, null, { id: 1 }].unique('id')
).toEqual([{ id: 1 }, , { id: 2 }, null]);
});
});
8 changes: 6 additions & 2 deletions polyfill/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
type Resolver<T> = (item: T) => any;

type ValueResolver<T> = number | string | symbol | Resolver<T>;
type Indexer<T> = number | keyof T | symbol;

type ValueResolver<T> = Indexer<T> | Resolver<T>;

interface Array<T> {
unique(valueResolver?: ValueResolver<T>): T[];
Expand All @@ -16,7 +18,9 @@ if (typeof Array.prototype.unique !== 'function')
const key = typeof valueResolver !== 'function' && valueResolver,
map = new Map<any, T>();

valueResolver = key ? item => item[key] : valueResolver;
valueResolver = key
? (item: Record<Indexer<T>, any>) => item?.[key] ?? item
: valueResolver;

for (const item of this)
map.set((valueResolver as Resolver<T>)(item), item);
Expand Down

0 comments on commit dfab433

Please sign in to comment.