Skip to content

Commit

Permalink
is-shallow-equal: Convert to ESM (#26833)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sarayourfriend authored Nov 13, 2020
1 parent 132a69f commit a109fda
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 65 deletions.
22 changes: 0 additions & 22 deletions packages/is-shallow-equal/.eslintrc.json

This file was deleted.

4 changes: 4 additions & 0 deletions packages/is-shallow-equal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/is-shallow-equal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
2 changes: 1 addition & 1 deletion packages/is-shallow-equal/benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand Down
8 changes: 6 additions & 2 deletions packages/is-shallow-equal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

/**
* Returns true if the two arrays are shallow equal, or false otherwise.
*
Expand All @@ -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;
}
Expand All @@ -19,13 +15,11 @@ 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;
}
}

return true;
}

module.exports = isShallowEqualArrays;
Original file line number Diff line number Diff line change
@@ -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<string, any>} ComparableObject
Expand All @@ -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;
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
'use strict';

var keys = Object.keys;

/**
* Returns true if the two objects are shallow equal, or false otherwise.
*
Expand All @@ -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
Expand All @@ -47,5 +41,3 @@ function isShallowEqualObjects( a, b ) {

return true;
}

module.exports = isShallowEqualObjects;
2 changes: 1 addition & 1 deletion packages/is-shallow-equal/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import isShallowEqual, {
isShallowEqualArrays,
isShallowEqualObjects,
} from '..';
} from '../src';

describe( 'isShallowEqual', () => {
it( 'returns false if of different types', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/is-shallow-equal/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "lib",
"rootDir": "src",
"declarationDir": "build-types"
},
"include": [ "lib/**/*" ]
"include": [ "src/**/*" ]
}

0 comments on commit a109fda

Please sign in to comment.