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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(just-cartesian-product): enable esm #315

Merged
merged 11 commits into from
Oct 31, 2021
17 changes: 17 additions & 0 deletions config/createRollupConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const cjs = require('@rollup/plugin-commonjs');
const {resolve} = require('path');

/**
*
* @param {string} path
*/
module.exports = function createRollupConfig(path) {
return {
input: resolve(path, './index.js'),
output: {
file: resolve(path, './index.esm.js'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extension does not work for NodeJS' native esm implementation, the extension needs to be .mjs for NodeJS to run the file in ESM mode.

Right now when using NodeJS' native esm implementation, an error will be thrown when using import on any Just packages:

(node:55016) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/tmp/just-esm-example/node_modules/just-camel-case/index.esm.js:40
export {stringCamelCase as default};
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:196:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)

format: 'esm',
},
plugins: [cjs()],
};
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
"test": "test"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"eslint": "6.8.0",
"lerna": "3.20.2",
"rollup": "^2.58.0",
"tap-spec": "5.0.0",
"tape": "^4.0.0",
"typescript": "^4.2.3"
},
"scripts": {
"build": "lerna run build --stream $@",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this command allow us to run:

# for all packages
yarn build

# or filtering by package name
yarn build --scope=just-cartesian-product

"lint": "eslint .",
"test": "npm run lint && tape ./test/**/index.js | tap-spec; npm run test-types",
"test-types": "tsc packages/**/*.tests.ts --noEmit --strictNullChecks"
Expand All @@ -37,4 +40,4 @@
"dependencies": {
"test": "^0.6.0"
}
}
}
55 changes: 55 additions & 0 deletions packages/array-cartesian-product/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var arrayCartesianProduct = cartesianProduct;

/*
cartesianProduct([[1, 2], ['a', 'b']]); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
cartesianProduct(); // throws
*/

function isArray(item) {
if (Object.prototype.toString.call(item) === '[object Array]') {
return true;
}

return false;
}

function baseProduct(prevProduct, arr2) {
//pre allocate all our memory
var newProduct = new Array(prevProduct.length * arr2.length);

for (var i = 0; i < prevProduct.length; i++) {
for (var j = 0; j < arr2.length; j++) {
//always provide array to array.concat for consistent behavior
newProduct[i * arr2.length + j] = prevProduct[i].concat([arr2[j]]);
}
}
return newProduct;
}

function cartesianProduct(arr) {
if (!isArray(arr)) {
throw new Error('just-cartesian-product expects an array');
}

if (!arr.length) {
return [];
}

if(!isArray(arr[0])) {
throw new Error('set at index 0 must be an array');
}

//initialize our product array
var product = arr[0].map(function(v) { return [v]; });

for (var i = 1; i < arr.length; i++) {
if(!isArray(product)) {
throw new Error('set at index ' + i + ' must be an array');
}
product = baseProduct(product, arr[i]);
}

return product;
}

export { arrayCartesianProduct as default };
13 changes: 11 additions & 2 deletions packages/array-cartesian-product/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
"name": "just-cartesian-product",
"version": "3.0.0",
"description": "Cartesian product of arrays",
"type": "commonjs",
"main": "index.js",
"module": "index.esm.js",
"exports": {
".": {
"require": "./index.js",
"default": "./index.esm.js"
}
},
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c"
},
"repository": "https://github.com/angus-c/just",
"keywords": [
Expand All @@ -21,4 +30,4 @@
"bugs": {
"url": "https://github.com/angus-c/just/issues"
}
}
}
3 changes: 3 additions & 0 deletions packages/array-cartesian-product/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const createRollupConfig = require('../../config/createRollupConfig');

module.exports = createRollupConfig(__dirname);
25 changes: 25 additions & 0 deletions packages/array-compact/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var arrayCompact = compact;

/*
compact([1, null, 2, undefined, null, NaN, 3, 4, false, 5]); // [1, 2, 3, 4, 5]
compact([1, 2, [], 4, {}]); // [1, 2, [], 4, {}]
compact([]); // []
compact({}); // throws
*/

function compact(arr) {
if (!Array.isArray(arr)) {
throw new Error('expected an array');
}
var result = [];
var len = arr.length;
for (var i = 0; i < len; i++) {
var elem = arr[i];
if (elem) {
result.push(elem);
}
}
return result;
}

export { arrayCompact as default };
11 changes: 10 additions & 1 deletion packages/array-compact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
"name": "just-compact",
"version": "2.3.0",
"description": "returns a copy of an array with falsey values removed",
"type": "commonjs",
"main": "index.js",
"module": "index.esm.js",
"exports": {
".": {
"require": "./index.js",
"default": "./index.esm.js"
}
},
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c"
},
"repository": "https://github.com/angus-c/just",
"keywords": [
Expand Down
3 changes: 3 additions & 0 deletions packages/array-compact/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const createRollupConfig = require('../../config/createRollupConfig');

module.exports = createRollupConfig(__dirname);
38 changes: 38 additions & 0 deletions packages/array-flatten/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var arrayFlatten = flatten;

/*
flatten([[1, [2, 3]], [[4, 5], 6, 7, [8, 9]]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
*/

function flattenHelper(arr, depth) {
var stack = arr.slice();
var result = [];

while (stack.length) {
var item = stack.pop();

if (Array.isArray(item) && depth > 0) {
stack.push.apply(stack, item);
depth--;
} else {
result.push(item);
}
}

return result.reverse();
}

function flatten(arr, depth) {
if (!Array.isArray(arr)) {
throw new Error('expected an array');
}

if (depth !== undefined && typeof depth !== 'number') {
throw new Error('depth expects a number');
}

return flattenHelper(arr, typeof depth === 'number' ? depth : Infinity);
}

export { arrayFlatten as default };
13 changes: 11 additions & 2 deletions packages/array-flatten/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
"name": "just-flatten-it",
"version": "4.1.0",
"description": "return a flattened array",
"type": "commonjs",
"main": "index.js",
"module": "index.esm.js",
"exports": {
".": {
"require": "./index.js",
"default": "./index.esm.js"
}
},
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c"
},
"repository": "https://github.com/angus-c/just",
"keywords": [
Expand All @@ -19,4 +28,4 @@
"bugs": {
"url": "https://github.com/angus-c/just/issues"
}
}
}
3 changes: 3 additions & 0 deletions packages/array-flatten/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const createRollupConfig = require('../../config/createRollupConfig');

module.exports = createRollupConfig(__dirname);
28 changes: 28 additions & 0 deletions packages/array-group-by/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var arrayGroupBy = groupBy;

function groupBy(arr, cb) {
if (!Array.isArray(arr)) {
throw new Error('expected an array for first argument');
}

if (typeof cb !== 'function') {
throw new Error('expected a function for second argument');
}

var result = {};
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
var bucketCategory = cb(item);
var bucket = result[bucketCategory];

if (!Array.isArray(bucket)) {
result[bucketCategory] = [item];
} else {
result[bucketCategory].push(item);
}
}

return result;
}

export { arrayGroupBy as default };
11 changes: 10 additions & 1 deletion packages/array-group-by/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
"name": "just-group-by",
"version": "1.2.0",
"description": "return a grouped object from array",
"type": "commonjs",
"main": "index.js",
"module": "index.esm.js",
"exports": {
".": {
"require": "./index.js",
"default": "./index.esm.js"
}
},
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c"
},
"repository": "https://github.com/angus-c/just",
"keywords": [
Expand Down
3 changes: 3 additions & 0 deletions packages/array-group-by/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const createRollupConfig = require('../../config/createRollupConfig');

module.exports = createRollupConfig(__dirname);
31 changes: 31 additions & 0 deletions packages/array-index/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var arrayIndex = index;

/*
index([{id: 'first', val: 1}, {id: 'second', val: 2}], 'id');
// {first: {id: 'first', val: 1}, second: {id: 'second', val: 2}}
index([{id: 'first', val: 1}, null], 'id'); // {first: {id: 'first', val: 1}}
index([], 'id'); // {}
index([], null); // throws
index({}, 'id'); // throws
*/

function index(arr, key) {
if (!Array.isArray(arr)) {
throw new Error('expected an array for first argument');
}
if (typeof key != 'string') {
throw new Error('expected a string for second argument');
}
var result = {};
var len = arr.length;
for (var i = 0; i < len; i++) {
var index = arr[i] && arr[i][key];

if (index) {
result[index] = arr[i];
}
}
return result;
}

export { arrayIndex as default };
13 changes: 11 additions & 2 deletions packages/array-index/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
"name": "just-index",
"version": "3.1.0",
"description": "return an object from an array, keyed by the value at the given id",
"type": "commonjs",
"main": "index.js",
"module": "index.esm.js",
"exports": {
".": {
"require": "./index.js",
"default": "./index.esm.js"
}
},
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c"
},
"repository": "https://github.com/angus-c/just",
"keywords": [
Expand All @@ -19,4 +28,4 @@
"bugs": {
"url": "https://github.com/angus-c/just/issues"
}
}
}
3 changes: 3 additions & 0 deletions packages/array-index/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const createRollupConfig = require('../../config/createRollupConfig');

module.exports = createRollupConfig(__dirname);
28 changes: 28 additions & 0 deletions packages/array-insert/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var arrayInsert = insert;

/*
insert([1, 2, 5, 6], ['a', 'c', 'e'], 2); // [1, 2, 'a', 'c', 'e', 5, 6]
insert([1, 2, 5, 6], 'a', 2); // [1, 2, 'a', 5, 6]
insert([1, 2, 5, 6], ['a', 'c', 'e'], 0); // ['a', 'c', 'e', 1, 2, 5, 6]
insert([1, 2, 5, 6], ['a', 'c', 'e']); // ['a', 'c', 'e', 1, 2, 5, 6]
*/

function insert(arr1, arr2, index) {
if (!Array.isArray(arr1)) {
throw new Error('expected an array for first argument');
}
if (arguments.length > 2 && typeof index != 'number') {
throw new Error('expected a number for third argument');
}
if (!Array.isArray(arr2)) {
arr2 = [arr2];
}
if (!index) {
return arr2.concat(arr1);
}
var front = arr1.slice(0, index);
var back = arr1.slice(index);
return front.concat(arr2, back);
}

export { arrayInsert as default };