-
Notifications
You must be signed in to change notification settings - Fork 207
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4d147c7
feat(just-cartesian-product): enable esm
raulfdm eb1a17d
chore: add rollup as comparison
raulfdm 3cb4db3
chore: remove esbuild
raulfdm a1acc12
feat: create rollup config factory
raulfdm 43cd2c2
chore: replace index.esm with rollup output
raulfdm 972ac9f
chore: create rollup config for all packages
raulfdm d2de48e
temp npmrc
raulfdm c335065
feat: add support to ESM
raulfdm 4ace013
chore: remove .npmrc
raulfdm 3371b32
Merge branch 'master' into backup-master
raulfdm b011476
Merge pull request #1 from raulfdm/backup-master
raulfdm File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,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'), | ||
format: 'esm', | ||
}, | ||
plugins: [cjs()], | ||
}; | ||
}; |
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 |
---|---|---|
|
@@ -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 $@", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
@@ -37,4 +40,4 @@ | |
"dependencies": { | ||
"test": "^0.6.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,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 }; |
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
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 @@ | ||
const createRollupConfig = require('../../config/createRollupConfig'); | ||
|
||
module.exports = createRollupConfig(__dirname); |
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,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 }; |
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
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 @@ | ||
const createRollupConfig = require('../../config/createRollupConfig'); | ||
|
||
module.exports = createRollupConfig(__dirname); |
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,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 }; |
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
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 @@ | ||
const createRollupConfig = require('../../config/createRollupConfig'); | ||
|
||
module.exports = createRollupConfig(__dirname); |
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,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 }; |
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
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 @@ | ||
const createRollupConfig = require('../../config/createRollupConfig'); | ||
|
||
module.exports = createRollupConfig(__dirname); |
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,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 }; |
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
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 @@ | ||
const createRollupConfig = require('../../config/createRollupConfig'); | ||
|
||
module.exports = createRollupConfig(__dirname); |
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,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 }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: