-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @module object-loops/inverse | ||
*/ | ||
var isInteger = require('101/is-integer') | ||
var reduce = require('./reduce') | ||
|
||
/** | ||
* Creates a new object with keys and values swapped. | ||
* @function module:object-loops/inverse | ||
* @param {object} [obj] - object to inverse keys and values, not accepted if being used directly on Object.prototype | ||
* @returns {object} newly created object with inverseped values | ||
*/ | ||
module.exports = inverse | ||
|
||
function inverse (obj) { | ||
if (Array.isArray(obj)) { | ||
var valsAreInts = obj.every(isInteger) | ||
return obj.reduce(flip, valsAreInts ? [] : {}) | ||
} | ||
if (typeof obj !== 'object' && typeof obj !== 'function') { | ||
throw new TypeError(obj + ' must be an object') | ||
} | ||
return reduce(obj, flip, {}) | ||
} | ||
|
||
function flip (inversed, val, key) { | ||
inversed[val] = key | ||
return inversed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
var methodNames = [ | ||
'every', | ||
'inverse', | ||
'filter', | ||
'find', | ||
'findKey', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
var Code = require('code') | ||
var Lab = require('lab') | ||
var lab = exports.lab = Lab.script() | ||
|
||
var describe = lab.describe | ||
var it = lab.it | ||
var before = lab.before | ||
var after = lab.after | ||
var expect = Code.expect | ||
|
||
var inverse = require('../inverse') | ||
|
||
describe('inverse', function () { | ||
describe('prototype', function () { | ||
before(function (done) { | ||
require('../index')() | ||
done() | ||
}) | ||
after(require('./fixtures/reset-object-prototype')) | ||
it('should iterate through all the key-value pairs in the object', function (done) { | ||
var obj = { | ||
foo: 1, | ||
bar: 2, | ||
baz: 3 | ||
} | ||
var ret = obj.inverse() | ||
expect(ret).to.deep.equal({ | ||
'1': 'foo', | ||
'2': 'bar', | ||
'3': 'baz' | ||
}) | ||
done() | ||
}) | ||
}) | ||
describe('require', function () { | ||
it('should iterate through all the key-value pairs in the object', function (done) { | ||
var obj = { | ||
foo: 1, | ||
bar: 2, | ||
baz: 3 | ||
} | ||
var ret = inverse(obj) | ||
expect(ret).to.deep.equal({ | ||
'1': 'foo', | ||
'2': 'bar', | ||
'3': 'baz' | ||
}) | ||
done() | ||
}) | ||
it('should iterate through all the key-value pairs in an array (integer values)', function (done) { | ||
var arr = [ | ||
2, | ||
4, | ||
6 | ||
] | ||
var ret = inverse(arr) | ||
expect(ret).to.deep.equal([ | ||
undefined, | ||
undefined, | ||
0, | ||
undefined, | ||
1, | ||
undefined, | ||
2 | ||
]) | ||
done() | ||
}) | ||
it('should iterate through all the key-value pairs in an array (non-integer values)', function (done) { | ||
var arr = [ | ||
2, | ||
'yo', | ||
6 | ||
] | ||
var ret = inverse(arr) | ||
expect(ret).to.deep.equal({ | ||
'2': 0, | ||
'yo': 1, | ||
'6': 2 | ||
}) | ||
done() | ||
}) | ||
describe('errors', function () { | ||
it('should throw an error if obj must be an object', function (done) { | ||
var obj = 'notObject' | ||
var fn = inverse.bind(null, obj) | ||
expect(fn).to.throw(/must be an object/) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |