From 24b51e0e9430ea2ce4f072fb3413a88596b877eb Mon Sep 17 00:00:00 2001 From: tjmehta Date: Sun, 21 Aug 2016 01:46:31 -0700 Subject: [PATCH] added values-in w/ tests --- test/values-in.js | 43 +++++++++++++++++++++++++++++++++++++++++++ values-in.js | 17 +++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 test/values-in.js create mode 100644 values-in.js diff --git a/test/values-in.js b/test/values-in.js new file mode 100644 index 0000000..2cae8d0 --- /dev/null +++ b/test/values-in.js @@ -0,0 +1,43 @@ +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 valuesIn = require('../values-in') + +describe('valuesIn', function () { + function Person (name) { + this.name = name + } + Person.prototype.getName = function () { + return this.name + } + + describe('prototype', function () { + before(function (done) { + require('../index')() + done() + }) + after(require('./fixtures/reset-object-prototype')) + it('should get all direct enumerables valuesIn from object', function (done) { + var objValues = new Person('hey').valuesIn() + // assertions + expect(objValues).to.deep.equal(['hey', Person.prototype.getName]) + done() + }) + }) + + describe('require', function () { + it('should get all direct enumerables valuesIn from object', function (done) { + var objValues = valuesIn(new Person('hey')) + // assertions + expect(objValues).to.deep.equal(['hey', Person.prototype.getName]) + done() + }) + }) +}) diff --git a/values-in.js b/values-in.js new file mode 100644 index 0000000..81f45a5 --- /dev/null +++ b/values-in.js @@ -0,0 +1,17 @@ +/** + * @module object-loops/values-in + */ +var keysIn = require('101/keys-in') +/** + * Like `keysIn`, but for values. Includes enumerable key's values from the prototype chain. + * @function module:object-loops/values-in + * @param {object} [obj] - object to get all (incl prototype) enumerable key's values from + * @returns {array} values + */ +module.exports = valuesIn + +function valuesIn (obj) { + return keysIn(obj).map(function (key) { + return obj[key] + }) +}