From a109fda769bfc3e33dc6dfd7c454afb9c8c60fb8 Mon Sep 17 00:00:00 2001 From: Sara Marcondes Date: Fri, 13 Nov 2020 07:26:36 -0800 Subject: [PATCH] is-shallow-equal: Convert to ESM (#26833) * is-shallow-equal: Convert to ESM * is-shallow-equal: Fix test import for ci * Fix react-native module resolution * Clean up unnecessary code. * Update files entry * Update benchmark instructions and CHANGELOG --- packages/is-shallow-equal/.eslintrc.json | 22 ------------------- packages/is-shallow-equal/CHANGELOG.md | 4 ++++ packages/is-shallow-equal/README.md | 6 ++--- packages/is-shallow-equal/benchmark/index.js | 2 +- packages/is-shallow-equal/package.json | 8 +++++-- .../is-shallow-equal/{lib => src}/arrays.js | 10 ++------- .../is-shallow-equal/{lib => src}/index.js | 19 ++++++---------- .../is-shallow-equal/{lib => src}/objects.js | 20 +++++------------ packages/is-shallow-equal/test/index.js | 2 +- packages/is-shallow-equal/tsconfig.json | 4 ++-- 10 files changed, 32 insertions(+), 65 deletions(-) delete mode 100644 packages/is-shallow-equal/.eslintrc.json rename packages/is-shallow-equal/{lib => src}/arrays.js (73%) rename packages/is-shallow-equal/{lib => src}/index.js (58%) rename packages/is-shallow-equal/{lib => src}/objects.js (77%) diff --git a/packages/is-shallow-equal/.eslintrc.json b/packages/is-shallow-equal/.eslintrc.json deleted file mode 100644 index 2e49bacccbbdb..0000000000000 --- a/packages/is-shallow-equal/.eslintrc.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "root": true, - "extends": [ - "plugin:@wordpress/eslint-plugin/es5" - ], - "env": { - "node": true - }, - "settings": { - "jsdoc": { - "mode": "typescript" - } - }, - "overrides": [ - { - "files": [ "@(test|benchmark)/*.js" ], - "extends": [ - "plugin:@wordpress/eslint-plugin/recommended" - ] - } - ] -} diff --git a/packages/is-shallow-equal/CHANGELOG.md b/packages/is-shallow-equal/CHANGELOG.md index 0501783db3f36..bdd95fb63468e 100644 --- a/packages/is-shallow-equal/CHANGELOG.md +++ b/packages/is-shallow-equal/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Breaking Change + +- Re-write using ES Modules causing CJS default import to change from `require('@wordpress/is-shallow-equal)` to `require('@wordpress/is-shallow-equal).default`. ([#26833](https://github.com/WordPress/gutenberg/pull/26833)) + ## 2.0.0 (2020-04-15) ### Breaking Change diff --git a/packages/is-shallow-equal/README.md b/packages/is-shallow-equal/README.md index 04421cfa1408f..95837e95a5c72 100644 --- a/packages/is-shallow-equal/README.md +++ b/packages/is-shallow-equal/README.md @@ -122,10 +122,10 @@ The following results were produced under Node v10.15.3 (LTS) on a MacBook Pro ( You can run the benchmarks yourselves by cloning the repository, installing dependencies, and running the `benchmark/index.js` script: ``` -git clone https://github.com/WordPress/packages.git -cd packages/packages/is-shallow-equal +git clone https://github.com/WordPress/gutenberg.git npm install -node benchmark +npm run build:packages +node ./packages/is-shallow-equal/benchmark ```

Code is Poetry.

diff --git a/packages/is-shallow-equal/benchmark/index.js b/packages/is-shallow-equal/benchmark/index.js index ca86ac7b4db6f..3564146321c50 100644 --- a/packages/is-shallow-equal/benchmark/index.js +++ b/packages/is-shallow-equal/benchmark/index.js @@ -43,7 +43,7 @@ Promise.all( [ require( '..' ).isShallowEqualObjects, require( '..' ).isShallowEqualArrays, ], - [ '@wordpress/is-shallow-equal', require( '..' ) ], + [ '@wordpress/is-shallow-equal', require( '..' ).default ], [ 'shallowequal', shallowequal ], [ 'shallow-equal (type specific)', diff --git a/packages/is-shallow-equal/package.json b/packages/is-shallow-equal/package.json index d1cd9c45e5270..515b9bbe0ad48 100644 --- a/packages/is-shallow-equal/package.json +++ b/packages/is-shallow-equal/package.json @@ -21,11 +21,15 @@ "url": "https://github.com/WordPress/gutenberg/issues" }, "files": [ - "lib", + "build", + "build-module", "build-types", + "src", "*.md" ], - "main": "lib/index.js", + "main": "build/index.js", + "module": "build-module/index.js", + "react-native": "src/index", "types": "build-types", "sideEffects": false, "dependencies": { diff --git a/packages/is-shallow-equal/lib/arrays.js b/packages/is-shallow-equal/src/arrays.js similarity index 73% rename from packages/is-shallow-equal/lib/arrays.js rename to packages/is-shallow-equal/src/arrays.js index 6937373a98546..88adc7617e3bb 100644 --- a/packages/is-shallow-equal/lib/arrays.js +++ b/packages/is-shallow-equal/src/arrays.js @@ -1,5 +1,3 @@ -'use strict'; - /** * Returns true if the two arrays are shallow equal, or false otherwise. * @@ -8,9 +6,7 @@ * * @return {boolean} Whether the two arrays are shallow equal. */ -function isShallowEqualArrays( a, b ) { - var i; - +export default function isShallowEqualArrays( a, b ) { if ( a === b ) { return true; } @@ -19,7 +15,7 @@ function isShallowEqualArrays( a, b ) { return false; } - for ( i = 0; i < a.length; i++ ) { + for ( let i = 0, len = a.length; i < len; i++ ) { if ( a[ i ] !== b[ i ] ) { return false; } @@ -27,5 +23,3 @@ function isShallowEqualArrays( a, b ) { return true; } - -module.exports = isShallowEqualArrays; diff --git a/packages/is-shallow-equal/lib/index.js b/packages/is-shallow-equal/src/index.js similarity index 58% rename from packages/is-shallow-equal/lib/index.js rename to packages/is-shallow-equal/src/index.js index 3469656d02037..fddd62a022a3d 100644 --- a/packages/is-shallow-equal/lib/index.js +++ b/packages/is-shallow-equal/src/index.js @@ -1,12 +1,11 @@ -'use strict'; - /** - * Internal dependencies; + * Internal dependencies */ -var isShallowEqualObjects = require( './objects' ); -var isShallowEqualArrays = require( './arrays' ); +import isShallowEqualObjects from './objects'; +import isShallowEqualArrays from './arrays'; -var isArray = Array.isArray; +export { default as isShallowEqualObjects } from './objects'; +export { default as isShallowEqualArrays } from './arrays'; /** * @typedef {Record} ComparableObject @@ -21,18 +20,14 @@ var isArray = Array.isArray; * * @return {boolean} Whether the two values are shallow equal. */ -function isShallowEqual( a, b ) { +export default function isShallowEqual( a, b ) { if ( a && b ) { if ( a.constructor === Object && b.constructor === Object ) { return isShallowEqualObjects( a, b ); - } else if ( isArray( a ) && isArray( b ) ) { + } else if ( Array.isArray( a ) && Array.isArray( b ) ) { return isShallowEqualArrays( a, b ); } } return a === b; } - -module.exports = isShallowEqual; -module.exports.isShallowEqualObjects = isShallowEqualObjects; -module.exports.isShallowEqualArrays = isShallowEqualArrays; diff --git a/packages/is-shallow-equal/lib/objects.js b/packages/is-shallow-equal/src/objects.js similarity index 77% rename from packages/is-shallow-equal/lib/objects.js rename to packages/is-shallow-equal/src/objects.js index 2c7583aba9ce1..e2dc6867c7d51 100644 --- a/packages/is-shallow-equal/lib/objects.js +++ b/packages/is-shallow-equal/src/objects.js @@ -1,7 +1,3 @@ -'use strict'; - -var keys = Object.keys; - /** * Returns true if the two objects are shallow equal, or false otherwise. * @@ -10,25 +6,23 @@ var keys = Object.keys; * * @return {boolean} Whether the two objects are shallow equal. */ -function isShallowEqualObjects( a, b ) { - var aKeys, bKeys, i, key, aValue; - +export default function isShallowEqualObjects( a, b ) { if ( a === b ) { return true; } - aKeys = keys( a ); - bKeys = keys( b ); + const aKeys = Object.keys( a ); + const bKeys = Object.keys( b ); if ( aKeys.length !== bKeys.length ) { return false; } - i = 0; + let i = 0; while ( i < aKeys.length ) { - key = aKeys[ i ]; - aValue = a[ key ]; + const key = aKeys[ i ]; + const aValue = a[ key ]; if ( // In iterating only the keys of the first object after verifying @@ -47,5 +41,3 @@ function isShallowEqualObjects( a, b ) { return true; } - -module.exports = isShallowEqualObjects; diff --git a/packages/is-shallow-equal/test/index.js b/packages/is-shallow-equal/test/index.js index 66bf538552c64..ea99282111946 100644 --- a/packages/is-shallow-equal/test/index.js +++ b/packages/is-shallow-equal/test/index.js @@ -4,7 +4,7 @@ import isShallowEqual, { isShallowEqualArrays, isShallowEqualObjects, -} from '..'; +} from '../src'; describe( 'isShallowEqual', () => { it( 'returns false if of different types', () => { diff --git a/packages/is-shallow-equal/tsconfig.json b/packages/is-shallow-equal/tsconfig.json index 426ab13d0aa8f..3c2c31f506f13 100644 --- a/packages/is-shallow-equal/tsconfig.json +++ b/packages/is-shallow-equal/tsconfig.json @@ -1,8 +1,8 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "rootDir": "lib", + "rootDir": "src", "declarationDir": "build-types" }, - "include": [ "lib/**/*" ] + "include": [ "src/**/*" ] }