Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
feat(prop): expose prop as a function
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
damoclark authored and thetutlage committed Dec 2, 2017
1 parent 47484c5 commit d12069d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -65,6 +77,7 @@ pope('Hello {{ username }}', {}, {




## The MIT License

Copyright (c) 2015 Harminder Virk
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/pope.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down

0 comments on commit d12069d

Please sign in to comment.