Skip to content

Commit

Permalink
add inverse
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmehta committed Mar 18, 2016
1 parent a55b5e0 commit 84f241c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ module.exports = extendObjectPrototype
function extendObjectPrototype (hideWarnings) {
[
'every',
'inverse',
'filter',
'find',
'findKey',
'find',
'forEach',
'map',
'mapKeys',
'map',
'reduce',
'some'
].forEach(function (methodName) {
Expand Down
29 changes: 29 additions & 0 deletions inverse.js
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
}
1 change: 1 addition & 0 deletions test/fixtures/reset-object-prototype.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var methodNames = [
'every',
'inverse',
'filter',
'find',
'findKey',
Expand Down
91 changes: 91 additions & 0 deletions test/test-inverse.js
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()
})
})
})
})

0 comments on commit 84f241c

Please sign in to comment.