Skip to content
Permalink
Browse files

feat: add create clumpit function

  • Loading branch information...
Joshua Thornton
Joshua Thornton committed Apr 10, 2019
1 parent f9bd41a commit c6906aab03465461527356c00e36bd4696a622fe
Showing with 5,804 additions and 1 deletion.
  1. +3 −0 .babelrc
  2. +3 −0 .gitignore
  3. +32 −1 README.md
  4. +5 −0 commitlint.config.js
  5. +37 −0 package.json
  6. +9 −0 release.config.js
  7. +24 −0 rollup.config.js
  8. +61 −0 src/index.ts
  9. +5,630 −0 yarn.lock
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-typescript"]
}
@@ -0,0 +1,3 @@
/node_modules/
/dist/
yarn-error.log
@@ -1 +1,32 @@
# clumpit
# clumpit

## Usage

```js
import createClumpit from 'clumpit';
const clumpit = createClumpit({
dedupe: true,
});
const a = clumpit.add('1', {
foo: 'bar',
});
const b = clumpit.add('1', {
foo: 'quz',
});
const c = clumpit.add('2', {
foo: 'baz',
});
const d = clumpit.run((items) => {
return Promise.resolve({
'1': 'x',
'2': 'y',
});
});
```
@@ -0,0 +1,5 @@
module.exports = {
extends: [
'@commitlint/config-conventional',
],
};
@@ -0,0 +1,37 @@
{
"name": "clumpit",
"version": "0.0.0",
"main": "dist/cjs/clumpit.js",
"module": "dist/es/clumpit.js",
"repository": "git@github.com:team-griffin/clumpit.git",
"author": "Joshua Thornton <jthornton1@godaddy.com>",
"license": "MIT",
"scripts": {
"build": "rollup --config ./rollup.config.js",
"prepublishOnly": "yarn build"
},
"devDependencies": {
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/node": "^7.2.2",
"@babel/preset-typescript": "^7.3.3",
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@semantic-release/changelog": "^3.0.2",
"@semantic-release/git": "^7.0.8",
"husky": "^1.3.1",
"rollup": "^1.9.0",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-local-resolve": "^1.0.7",
"semantic-release": "^15.13.3"
},
"dependencies": {
"lodash.keyby": "^4.6.0",
"lodash.uniqby": "^4.7.0"
},
"husky": {
"hooks": {
"commit-msg": "commitlint --edit"
}
}
}
@@ -0,0 +1,9 @@
module.exports = {
plugins: [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/git",
],
};
@@ -0,0 +1,24 @@
import babel from 'rollup-plugin-babel';
import localResolve from 'rollup-plugin-local-resolve';

export default {
input: 'src/index.ts',
output: [
{
file: 'dist/es/clumpit.js',
format: 'es',
},
{
file: 'dist/cjs/clumpit.js',
format: 'cjs',
},
],
plugins: [
localResolve(),
babel({
exclude: 'node_modules/**',
// plugins: ['external-helpers'],
extensions: ['.js', '.jsx', '.ts', '.tsx'],
}),
],
};
@@ -0,0 +1,61 @@
import uniqueBy from 'lodash.uniqby';
import keyBy from 'lodash.keyby';

type Item = {
id: string,
data: Object,
resolve: () => void,
reject: () => void,
};

export type RunFunction<R> = (items: Array<Item>) => Promise<R>;
export type Options = {
dedupe: boolean,
};

export type Clumpit = {
add: <T>(id: string, data: Object) => Promise<T>,
run: <T>(fn: RunFunction<T>) => Promise<void>,
};

function dedupe(items) {
return uniqueBy(items, (item) => item.id);
}

function createClumpit(options: Options): Clumpit {
const items: Array<Item> = [];

function add<T>(id: string, data: Object): Promise<T> {
return new Promise((resolve, reject) => {
items.push({
id,
data,
resolve,
reject,
});
});
}

function run<R>(fn: RunFunction<R>): Promise<void> {
const deduped = dedupe(items);

return fn(items).then((results) => {
const keydItems = keyBy(deduped, (item) => item.id);

for(const id in results) {
const result = results[id];

keydItems[id].resolve(result);
}

return Promise.resolve();
});
}

return {
add,
run,
};
}

export default createClumpit;

0 comments on commit c6906aa

Please sign in to comment.
You can’t perform that action at this time.