Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.

Commit

Permalink
No longer calling transformations multiple times for array values.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluma committed Mar 24, 2014
1 parent 3dac841 commit 4797f61
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
22 changes: 8 additions & 14 deletions README.md
Expand Up @@ -31,30 +31,24 @@ make test

Transforms the given object by mapping it against the given transformation recursively.

If `obj` is an array and `transformation` is an array, returns an array of the results of applying each transformation in the `transformation` array to the respective object in the `obj` array:
If `obj` is an object and `transformation` is an object, returns a new object with each property set to the result of applying the property of the `transformation` to the respective property of the `obj`:

```javascript
function upper(s) {return s.toUpperCase();}
function lower(s) {return s.toLowerCase();}
var result = transform(['Foo', 'Bar', 'Qux'], [upper, lower]);
console.log(result); // ['FOO', 'bar', 'Qux']
```

If `obj` is an array and `transformation` is not an array, returns an array of the results of applying the `transformation` to each object in the `obj` array:

```javascript
function upper(s) {return s.toUpperCase();}
var result = transform(['Foo', 'Bar', 'Qux'], upper);
console.log(result); // ['FOO', 'BAR', 'QUX']
var result = transform({a: 'Foo', b: 'Bar', c: 'Qux'}, {a: upper, b: lower});
console.log(result); // {a: 'FOO', b: 'bar', c: 'Qux'}
```

If `obj` is an object and `transformation` is an object, returns a new object with each property set to the result of applying the property of the `transformation` to the respective property of the `obj`:
If `obj` is an array and `transformation` is an array or object, returns a new array with each item set to the result of applying the matching property or item of the `transformation` to the respective item in the `obj` array:

```javascript
function upper(s) {return s.toUpperCase();}
function lower(s) {return s.toLowerCase();}
var result = transform({a: 'Foo', b: 'Bar', c: 'Qux'}, {a: upper, b: lower});
console.log(result); // {a: 'FOO', b: 'bar', c: 'Qux'}
var result1 = transform(['Foo', 'Bar', 'Qux'], {0: upper, 2: lower});
console.log(result1); // ['FOO', 'Bar', 'qux']
var result2 = transform(['Foo', 'Bar', 'Qux'], [upper, undefined, lower]);
console.log(result2); // ['FOO', 'Bar', 'qux']
```

If `transformation` is a `Function`, returns the result of calling it with the given `obj` as argument:
Expand Down
11 changes: 2 additions & 9 deletions index.js
@@ -1,16 +1,9 @@
/*! transform-object 0.1.2 Original author Alan Plum <me@pluma.io>. Released into the Public Domain under the UNLICENSE. @preserve */
/*! transform-object 0.2.0 Original author Alan Plum <me@pluma.io>. Released into the Public Domain under the UNLICENSE. @preserve */
module.exports = transform;

function transform(obj, transformer) {
if (Array.isArray(obj)) {
return Array.isArray(transformer) ? obj.map(function(value, i) {
return transform(value, transformer[i]);
}) : obj.map(function(value) {
return transform(value, transformer);
});
}
if (typeof transformer === 'object' && typeof obj === 'object') {
var result = {};
var result = Array.isArray(obj) ? [] : {};
Object.keys(obj).forEach(function(key) {
result[key] = transform(obj[key], transformer[key]);
});
Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "transform-object",
"version": "0.1.2",
"version": "0.2.0",
"author": "Alan Plum <me@pluma.io>",
"description": "Transforms objects.",
"licenses": {
Expand All @@ -21,11 +21,11 @@
"dependencies": {
},
"devDependencies": {
"mocha": "~1.17",
"mocha": "~1.18",
"expect.js": "~0.3",
"jshint": "~2.4",
"istanbul": "~0.2",
"coveralls": "~2.8"
"coveralls": "~2.10"
},
"main": "index.js",
"scripts": {
Expand Down
35 changes: 27 additions & 8 deletions spec/transform.spec.js
Expand Up @@ -4,21 +4,25 @@ var expect = require('expect.js'),

function upper(s) {return s.toUpperCase();}
function lower(s) {return s.toLowerCase();}
function map(fn) {return function(arr) {return arr.map(fn);};}

describe('transform(Array, Array)', function() {
it('returns an array', function() {
expect(transform([], [])).to.be.an('array');
});
it('applies each transformation to each matching item', function() {
expect(transform(['Foo', 'Bar', 'Qux'], [upper, lower]))
.to.eql(['FOO', 'bar', 'Qux']);
expect(transform(['Foo', 'Bar', 'Qux'], [upper, undefined, lower]))
.to.eql(['FOO', 'Bar', 'qux']);
});
});

describe('transform(Array, *)', function() {
it('applies the transformation to each item', function() {
expect(transform(['Foo', 'Bar', 'Qux'], upper))
.to.eql(['FOO', 'BAR', 'QUX']);
describe('transform(Array, Object)', function() {
it('returns an array', function() {
expect(transform([], {})).to.be.an('array');
});
it('applies each transformation to each matching item', function() {
expect(transform(['Foo', 'Bar', 'Qux'], {0: upper, 2: lower}))
.to.eql(['FOO', 'Bar', 'qux']);
});
});

Expand All @@ -29,8 +33,23 @@ describe('transform(Object, Object)', function() {
});
});

describe('transform(Array, Function)', function() {
it('passes the array to the function', function() {
var called = false,
arg = [];
transform(arg, function(fnArg) {
expect(fnArg).to.equal(arg);
called = true;
});
expect(called).to.equal(true);
});
it('returns the result of the function', function() {
expect(transform(['foo', 'bar', 'qux'], map(upper))).to.eql(['FOO', 'BAR', 'QUX']);
});
});

describe('transform(*, Function)', function() {
it('passes the object to the function', function() {
it('passes the value to the function', function() {
var called = false,
arg = {};
transform(arg, function(fnArg) {
Expand All @@ -45,7 +64,7 @@ describe('transform(*, Function)', function() {
});

describe('transform(*, undefined)', function() {
it('returns the object', function() {
it('returns the value', function() {
var arg = {};
expect(transform(arg, undefined)).to.equal(arg);
});
Expand Down

0 comments on commit 4797f61

Please sign in to comment.