Socket
Socket
Sign inDemoInstall

datafactory

Package Overview
Dependencies
114
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.0 to 0.0.1

dist/datafactory.min.js

18

package.json
{
"name": "datafactory",
"version": "0.0.0",
"version": "0.0.1",
"description": "A lightweight node package for creating test / demo data and fixtures.",
"scripts": {
"precommit": "npm test && npm run build",
"clean": "rimraf dist",
"production": "npm run clean && set NODE_ENV=production && webpack --config ./config/webpack/webpack.production.config.js --progress --profile --colors",
"test": "npm run test:server && npm run test:browser",
"test": "npm run test:server",
"test:server": "mocha --opts config/mocha.opts",

@@ -81,11 +80,12 @@ "watch:server": "npm run test:server -- --watch",

"dependencies": {
"babel-core": "^6.1.21",
"dotenv": "^1.2.0",
"karma-chrome-launcher": "^0.2.1",
"karma-firefox-launcher": "^0.1.7",
"karma-sourcemap-loader": "^0.3.6",
"lodash": "^3.10.1",
"npmlog": "^2.0.0",
"rimraf": "^2.4.3",
"webpack-notifier": "1.2.1",
"karma-firefox-launcher": "^0.1.7",
"karma-chrome-launcher": "^0.2.1",
"babel-core": "^6.1.21",
"dotenv": "^1.2.0",
"karma-sourcemap-loader": "^0.3.6"
"webpack-notifier": "1.2.1"
}
}
# datafactory
A lightweight node package for creating test data, demo data and fixtures.
[![Travis Status][trav_img]][trav_site]
[![devDependency Status](https://david-dm.org/dispatchme/datafactory/dev-status.svg)](https://david-dm.org/dispatchme/datafactory)
[![Dependency Status](https://david-dm.org/dispatchme/datafactory.svg)](https://david-dm.org/dispatchme/datafactory)
> A lightweight node package for creating test data, demo data and fixtures.
# Example Usage

@@ -89,1 +93,5 @@

MIT © [Dispatch](https://github.com/dispatchme)
[trav_img]: https://api.travis-ci.org/DispatchMe/datafactory.svg
[trav_site]: https://travis-ci.org/DispatchMe/datafactory

@@ -0,5 +1,103 @@

import _ from 'lodash';
// Set the value to the namespace built from the path
// ex path='addresses.hq', value=1, set obj.addresses.hq = 1;
function parseDotNotation(path, val, obj) {
var currentObj = obj,
keys = path.split('.'),
i, l = Math.max(1, keys.length - 1),
key;
for (i = 0; i < l; ++i) {
key = keys[i];
currentObj[key] = currentObj[key] || {};
currentObj = currentObj[key];
}
currentObj[keys[i]] = val;
delete obj[path];
}
function buildValue(defaults, opts, group) {
let output = {};
// Allow the root level to be a function
if (_.isFunction(defaults)) {
output = defaults(group);
} else {
_.forOwn(defaults, (defaultVal, key) => {
if (_.isFunction(defaultVal)) {
output[key] = defaultVal(group);
} else {
output[key] = defaultVal;
}
});
}
return _.extend(output, opts);
}
class Group {
constructor(factory) {
const self = this;
self.data = {};
// Create builder functions
_.forOwn(factory._builders, (defaults, key) => {
const groupBuilder = (opts) => {
const output = buildValue(defaults, opts, self);
let dataKey = key;
// If there is a namespace (ex. 'address.hq') store the build docs on 'address'
if (dataKey.indexOf('.')) {
dataKey = key.split('.')[0];
}
// track the build doc in the group data
const builtDocs = self.data[dataKey] || [];
builtDocs.push(output);
self.data[dataKey] = builtDocs;
// Return the group to allow chaining
return self;
}
// Allow builders to be namespaced via '.'
// ex. 'addresses.hq' -> factory.addresses.hq();
if (key.indexOf('.') > -1) {
parseDotNotation(key, groupBuilder, self);
} else {
self[key] = groupBuilder;
}
});
}
}
export default class DataFactory {
constructor() {
console.log('Make me a factory damnit!');
constructor(builders) {
const self = this;
self._builders = builders;
// Create a builder function for each root key
_.forOwn(builders, (defaults, key) => {
const builder = (opts, group) => {
return buildValue(defaults, opts, null);
};
// Allow builders to be namespaced via '.'
// ex. 'addresses.hq' -> factory.addresses.hq();
if (key.indexOf('.') > -1) {
parseDotNotation(key, builder, self);
} else {
self[key] = builder;
}
});
}
createGroup() {
return new Group(this);
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc