From d12069d3d2255585d2f73df72279a957ace995b3 Mon Sep 17 00:00:00 2001 From: Damo Date: Sun, 3 Dec 2017 02:55:26 +1000 Subject: [PATCH] feat(prop): expose prop as a function * Expose the prop function as part of the API * Reverse the arguments to the prop function to mimic the pope function * Simplified exports, removed const, fixed tests and updated readme --- index.js | 5 ++++- readme.md | 13 +++++++++++++ src/index.js | 8 ++++---- test/pope.spec.js | 6 +++--- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 43a96fb..a6d2da2 100644 --- a/index.js +++ b/index.js @@ -6,4 +6,7 @@ * MIT Licensed */ -module.exports = require('./src/index').pope +var funcs = require('./src/index') + +exports = module.exports = funcs.pope +exports.prop = funcs.prop diff --git a/readme.md b/readme.md index a63cfec..8baba75 100644 --- a/readme.md +++ b/readme.md @@ -27,6 +27,18 @@ const pope = require('pope') pope('There are {{0}} emails in your inbox', [20]) ``` +### prop + +If you wish to retrieve values from a nested data structure without interpolating into a string, you can make use of `prop`. Note the missing `{{}}`. + +```javascript +const pope = require('pope') +pope.prop('count',{count:20}) // 20 +pope.prop('profile.name',{profile: {name:'virk', age: 26}}) // virk +pope.prop('0', [20]) // 20 +pope.prop('profile.validate',{profile: {name:'virk', validate: /^[A-Z][a-z]+/}}) // /^[A-Z][a-z]+/ +``` + ## Options You can also pass an options object to define the interpolation behavior. @@ -65,6 +77,7 @@ pope('Hello {{ username }}', {}, { + ## The MIT License Copyright (c) 2015 Harminder Virk diff --git a/src/index.js b/src/index.js index 4f47919..97b2a06 100644 --- a/src/index.js +++ b/src/index.js @@ -10,12 +10,12 @@ * @description get nested properties from a given * object using dot notation * @method prop - * @param {Object} obj * @param {String} path + * @param {Object} obj * @return {Mixed} * @public */ -var prop = function(obj, path) { +var prop = function(path, obj) { if (typeof(obj) !== 'object' || typeof path !== 'string') { return obj; } @@ -47,11 +47,11 @@ var pope = function (string, data, options) { while (result = regex.exec(string)){ var item = result[1].trim() if(item) { - var value = prop(data, item) + var value = prop(item, data) if (value !== undefined && value !== null) { formattedString = formattedString.replace(result[0], value) } else if (options.throwOnUndefined) { - const error = new Error('Missing value for ' + result[0]) + var error = new Error('Missing value for ' + result[0]) error.key = item error.code = 'E_MISSING_KEY' throw error diff --git a/test/pope.spec.js b/test/pope.spec.js index 76d7b74..fedcae4 100644 --- a/test/pope.spec.js +++ b/test/pope.spec.js @@ -15,17 +15,17 @@ const expect = chai.expect describe('pope', function () { it('should fetch properties from a given object', function () { - const name = prop({name:'virk'},'name') + const name = prop('name', {name:'virk'}) expect(name).to.equal('virk') }) it('should fetch nested properties from a given object', function () { - const name = prop({profile: {name:'virk'}},'profile.name') + const name = prop('profile.name', {profile: {name:'virk'}}) expect(name).to.equal('virk') }) it('should fetch nested properties from a given object using array index', function () { - const name = prop({users:['virk','nikk']},'users.1') + const name = prop('users.1', {users:['virk','nikk']}) expect(name).to.equal('nikk') })