Skip to content

Commit

Permalink
Merge pull request #4 from tamtakoe/master
Browse files Browse the repository at this point in the history
Added build for browsers
  • Loading branch information
Jokero committed May 18, 2016
2 parents ce89646 + 47214ef commit cb31eb3
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 5 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,30 @@ Transform object from one structure to another by using schema. It's useful in A
[![NPM version](https://img.shields.io/npm/v/mapper.js.svg)](https://npmjs.org/package/mapper.js)
[![Build status](https://img.shields.io/travis/Jokero/mapper.js.svg)](https://travis-ci.org/Jokero/mapper.js)

**Note:** This module will only work with Node.js >= 4.0.
**Note:** This module works in browsers and Node.js >= 4.0.

## Installation

```sh
npm install mapper.js
```

### Node.js
```js
const mapper = require('mapper.js');
```

### Browser
```
<script src="node_modules/mapper.js/dist/mapper.js">
```
or minified version
```
<script src="node_modules/mapper.js/dist/mapper.min.js">
```

You can use the module with AMD/CommonJS or just use `window.mapper`.

## Usage

There are two ways to use module:
Expand Down Expand Up @@ -157,6 +173,13 @@ const userSchema = {
const result = mapper(user, userSchema); // or mapper(userSchema)(user)
```

## Build

```sh
npm install
npm run build
```

## Tests

```sh
Expand Down
191 changes: 191 additions & 0 deletions dist/mapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mapper = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';

var map = require('./lib/map');

/**
* @param {Object|Object[]} schemaOrData
* @param {Object} [schema]
*
* @returns {Function|Object|Object[]}
*/
module.exports = function (schemaOrData, schema) {
if (!schema) {
return function (value) {
return map(value, schemaOrData);
};
}

return map(schemaOrData, schema);
};

},{"./lib/map":2}],2:[function(require,module,exports){
'use strict';

var mapObject = require('./mapObject');

/**
* @param {Object|Object[]} data
* @param {Object} schema
*
* @returns {Object|Object[]}
*/
module.exports = function (data, schema) {
if (data instanceof Array) {
return data.map(function (object) {
return mapObject(object, schema);
});
}

return mapObject(data, schema);
};

},{"./mapObject":3}],3:[function(require,module,exports){
'use strict';

var mapProperty = require('./mapProperty');

/**
* @param {Object} object
* @param {Object} schema
* @param {Object} [originalObject=object]
* @param {String[]} [path=[]]
*
* @returns {Object}
*/
module.exports = function (object, schema, originalObject, path) {
originalObject = originalObject || object;
path = path || [];

var mappedObject = {};

Object.keys(schema).forEach(function (propertyName) {
var propertySchema = schema[propertyName];
var propertyValue = object && object[propertyName];
var propertyPath = path.concat(propertyName);

var mappedPropertyValue = mapProperty(propertyValue, propertySchema, object, originalObject, propertyPath);

if (mappedPropertyValue !== undefined) {
mappedObject[propertyName] = mappedPropertyValue;
}
});

return mappedObject;
};

},{"./mapProperty":4}],4:[function(require,module,exports){
'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var getPropertyValue = require('./utils').getPropertyValue;

/**
* @param {*} value
* @param {*} schema
* @param {Object|Array} parentObject
* @param {Object} originalObject
* @param {String[]} path
*
* @returns {*}
*/
module.exports = function mapProperty(value, schema, parentObject, originalObject, path) {
if (schema === true || schema === '=') {
return value;
}

if (typeof schema === 'string') {
var propertyPath = schema.startsWith('=') ? schema.substr(1) : schema;
return getPropertyValue(originalObject, parentObject, propertyPath);
}

if (schema instanceof Function) {
return schema(value, parentObject, originalObject, path);
}

if (schema instanceof Array && value instanceof Array) {
var _ret = function () {
var mappedArray = [];
var itemSchema = schema[0];

value.forEach(function (item, i) {
var itemPath = path.concat(i);
var mappedItem = mapProperty(item, itemSchema, value, originalObject, itemPath);

if (mappedItem !== undefined) {
mappedArray.push(mappedItem);
}
});

return {
v: mappedArray
};
}();

if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}

if (schema instanceof Object) {
var mapObject = require('./mapObject');
return mapObject(value, schema, originalObject, path);
}
};

},{"./mapObject":3,"./utils":5}],5:[function(require,module,exports){
'use strict';

/**
* @param {Object} originalObject
* @param {Object} parentObject
* @param {String} path
*
* @returns {*}
*/

exports.getPropertyValue = function (originalObject, parentObject, path) {
var value = void 0;

if (path.startsWith('$.')) {
path = path.substr(2);
value = originalObject;
} else {
value = parentObject;
}

var pathParts = path.split('.');

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = pathParts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var pathPart = _step.value;

if (!(value instanceof Object)) {
return;
}

value = value[pathPart];
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

return value;
};

},{}]},{},[1])(1)
});
1 change: 1 addition & 0 deletions dist/mapper.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mapper.js",
"description": "Transform object from one structure to another by using schema. It's useful in API when you want to modify data before sending it to clients",
"version": "0.1.1",
"version": "0.2.0",

"author": {
"name": "Dmitry Kirilyuk",
Expand Down Expand Up @@ -29,11 +29,18 @@
},

"devDependencies": {
"mocha": "2.4.5",
"chai": "3.5.0"
"browserify": "13.0.1",
"babel-preset-es2015": "6.6.0",
"babelify": "7.3.0",
"uglify-js": "2.6.2",
"chai": "3.5.0",
"mocha": "2.4.5"
},

"scripts": {
"test": "mocha 'tests/**/*.js' --timeout 5000"
"test": "mocha 'tests/**/*.js' --timeout 5000",
"browserify": "browserify index.js --transform [ babelify --presets es2015 ] --standalone mapper --outfile dist/mapper.js",
"minify": "uglifyjs dist/mapper.js --output dist/mapper.min.js",
"build": "npm run browserify && npm run minify"
}
}

0 comments on commit cb31eb3

Please # to comment.