forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…50263) * lib: introduce `uniqueBy` helper extract `uniqueBy` into `js-utils` pkg and use TS remove comment about `null` and `undefined` values * package.json: set `sideEffects` to `false` * tsconfig.json: remove `DOM` and `DOM.Iterable` from lib * update types Co-authored-by: sarayourfriend <saram@fastmail.com> * change version to 1.0.0 Co-authored-by: sarayourfriend <saram@fastmail.com>
- Loading branch information
1 parent
339e0ad
commit 2b1c472
Showing
10 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
Add `uniqueBy` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# JavaScript utils | ||
|
||
This is a collection of general purpose JavaScript utility functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
preset: '../../test/packages/jest-preset.js', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "@automattic/js-utils", | ||
"version": "1.0.0", | ||
"description": "General purpose Javascript utility functions", | ||
"homepage": "https://github.com/Automattic/wp-calypso", | ||
"license": "GPL-2.0-or-later", | ||
"author": "Automattic Inc.", | ||
"sideEffects": false, | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"calypso:src": "src/index.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Automattic/wp-calypso.git", | ||
"directory": "packages/js-utils" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"types": "dist/types", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Automattic/wp-calypso/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build ./tsconfig.json ./tsconfig-cjs.json --clean && npx rimraf dist", | ||
"build": "tsc --build ./tsconfig.json ./tsconfig-cjs.json", | ||
"prepack": "yarn run clean && yarn run build", | ||
"watch": "tsc --build ./tsconfig.json --watch" | ||
}, | ||
"dependencies": { | ||
"tslib": "^2.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as uniqueBy } from './unique-by'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import uniqueBy from '../unique-by'; | ||
|
||
describe( 'uniqueBy()', () => { | ||
test( 'should remove duplicate values by identity by default (numbers)', () => { | ||
const values = [ 1, 2, 3, 1, 2, 4 ]; | ||
expect( uniqueBy( values ) ).toEqual( [ 1, 2, 3, 4 ] ); | ||
} ); | ||
|
||
test( 'should remove duplicate values by identity by default (strings)', () => { | ||
const values = [ 'a', 'b', 'c', 'd', 'a', 'b' ]; | ||
expect( uniqueBy( values ) ).toEqual( [ 'a', 'b', 'c', 'd' ] ); | ||
} ); | ||
|
||
test( 'should remove duplicate values by identity by default (mixed numbers & strings)', () => { | ||
const values = [ 1, 2, 'a', 'b', 1, 2, 'c', 'a' ]; | ||
expect( uniqueBy( values ) ).toEqual( [ 1, 2, 'a', 'b', 'c' ] ); | ||
} ); | ||
|
||
test( 'should be able to handle `null` and `undefined` values', () => { | ||
const values = [ null, null, undefined, undefined ]; | ||
expect( uniqueBy( values ) ).toEqual( [ null, undefined ] ); | ||
} ); | ||
|
||
test( 'should not remove anything if there are no duplicates', () => { | ||
const values = [ 1, 2, 'a', 'b' ]; | ||
expect( uniqueBy( values ) ).toEqual( [ 1, 2, 'a', 'b' ] ); | ||
} ); | ||
|
||
test( 'should accept a custom compare function (String.trim)', () => { | ||
const values = [ 'a', 'a ', ' a ', ' a', ' a' ]; | ||
const compare = ( a, b ) => a.trim() === b.trim(); | ||
expect( uniqueBy( values, compare ) ).toEqual( [ 'a' ] ); | ||
} ); | ||
|
||
test( 'should accept a custom compare function (parseInt)', () => { | ||
const values = [ 1, '1', 2, 3, '3' ]; | ||
const compare = ( a, b ) => parseInt( a ) === parseInt( b ); | ||
expect( uniqueBy( values, compare ) ).toEqual( [ 1, 2, 3 ] ); | ||
} ); | ||
|
||
test( 'should accept a custom compare function (objects)', () => { | ||
const values = [ { ID: 1 }, { ID: 1 }, { ID: 2 }, { ID: 3 } ]; | ||
const compare = ( a, b ) => a.ID === b.ID; | ||
expect( uniqueBy( values, compare ) ).toEqual( [ { ID: 1 }, { ID: 2 }, { ID: 3 } ] ); | ||
} ); | ||
|
||
test( 'should return a new (shallow) array', () => { | ||
const values = [ 1, '2', { ID: 1 } ]; | ||
expect( uniqueBy( values ) ).not.toBe( values ); | ||
expect( uniqueBy( values )[ 2 ] ).toBe( values[ 2 ] ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const defaultCompare = ( a: any, b: any ): boolean => a === b; | ||
|
||
function uniqueBy< T >( arr: T[], fn: ( a: T, b: T ) => boolean = defaultCompare ): T[] { | ||
return arr.reduce( ( acc, v ) => { | ||
if ( ! acc.some( ( x: any ) => fn( v, x ) ) ) acc.push( v ); | ||
return acc; | ||
}, [] as T[] ); | ||
} | ||
|
||
export default uniqueBy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "./tsconfig", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": false, | ||
"declarationDir": null, | ||
"outDir": "dist/cjs", | ||
"composite": false, | ||
"incremental": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES5", | ||
"lib": [ "ESNext" ], | ||
"baseUrl": ".", | ||
"module": "esnext", | ||
"allowJs": false, | ||
"jsx": "react", | ||
"declaration": true, | ||
"declarationDir": "dist/types", | ||
"outDir": "dist/esm", | ||
"rootDir": "src", | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"typeRoots": [ "../../node_modules/@types" ], | ||
"types": [ "node" ], | ||
"noEmitHelpers": true, | ||
"importHelpers": true, | ||
"composite": true | ||
}, | ||
"include": [ "src" ], | ||
"exclude": [ "**/test/*" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters