https://github.com/mixmaxhq/detonate-object
Generate object copies combinatorially
https://github.com/mixmaxhq/detonate-object
supported
Last synced: 10 months ago
JSON representation
Generate object copies combinatorially
- Host: GitHub
- URL: https://github.com/mixmaxhq/detonate-object
- Owner: mixmaxhq
- License: mit
- Created: 2017-12-08T22:20:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-20T18:47:32.000Z (over 2 years ago)
- Last Synced: 2025-07-26T11:54:48.010Z (11 months ago)
- Topics: supported
- Language: JavaScript
- Homepage: https://www.mixmax.com/careers
- Size: 93.8 KB
- Stars: 2
- Watchers: 26
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
detonate-object
===============
[](https://travis-ci.org/mixmaxhq/detonate-object)
Generates objects from an object with `$each` declarations in a combinatorial manner.
Has nothing to do with destroying objects.
Not particularly fast, but aims to be correct and unsurprising. Comparable to a
[naïve implementation][naive] with an incrementing variation offset.
Relies on language features available in Node from v6 on.
Install
-------
```sh
$ yarn add detonate-object
# or
$ npm install --save detonate-object
```
Usage
-----
```js
const detonate = require('detonate-object');
// We return an iterator from detonate. If you want an array, pass it to `Array.from`.
const iter = detonate({
$each: [{
browserName: 'safari',
platform: 'macOS',
version: {$each: [10, 11]}
}, {
browserName: {$each: ['firefox', 'chrome']},
version: {$each: ['latest', 'latest-1']}
}],
platform: {$each: ['linux', 'macOS', 'windows']},
build: 14
});
for (const obj of iter) {
// loops over the following objects, in no particular order:
{ platform: 'linux', browserName: 'chrome', version: 'latest', build: 14}
{ platform: 'linux', browserName: 'chrome', version: 'latest-1', build: 14}
{ platform: 'linux', browserName: 'firefox', version: 'latest', build: 14}
{ platform: 'linux', browserName: 'firefox', version: 'latest-1', build: 14}
{ platform: 'macOS', browserName: 'chrome', version: 'latest', build: 14}
{ platform: 'macOS', browserName: 'chrome', version: 'latest-1', build: 14}
{ platform: 'macOS', browserName: 'firefox', version: 'latest', build: 14}
{ platform: 'macOS', browserName: 'firefox', version: 'latest-1', build: 14}
{ platform: 'macOS', browserName: 'safari', version: 10, build: 14}
{ platform: 'macOS', browserName: 'safari', version: 11, build: 14}
{ platform: 'windows', browserName: 'chrome', version: 'latest', build: 14}
{ platform: 'windows', browserName: 'chrome', version: 'latest-1', build: 14}
{ platform: 'windows', browserName: 'firefox', version: 'latest', build: 14}
{ platform: 'windows', browserName: 'firefox', version: 'latest-1', build: 14}
}
```
### Merge conflicts
detonate-object does guarantees that fields will be taken first from a top-level `$each` declaration, and then from the body.
```js
detonate({
{$each: [{name: 'abc'}]},
name: 'def'
});
// From the above, detonate-object will only produce this:
// {name: 'abc'}
// It will not produce this:
// {name: 'def'}
```
### Custom iterables
We can also use custom iterables:
```js
function* generator() {
yield 'hello';
yield 'world';
}
const iterable = {
[Symbol.iterator]() {
return generator();
}
};
Array.from(detonate({
msg: {$each: iterable},
extra: 'data'
}));
// => [
// {msg: 'hello', extra: 'data'},
// {msg: 'world', extra: 'data'}
// ]
```
[naive]: https://github.com/mixmaxhq/detonate-object/blob/master/benchmarks/naive.js