Comparing version 1.3.1 to 1.4.0
{ | ||
"name": "quizas", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "A sort-of monad for reading values from objects", | ||
@@ -5,0 +5,0 @@ "main": "src/quizas", |
"use strict"; | ||
/*globals module */ | ||
var FIND_REGEX = /([\w]+)([=])"([\w ]+)"/i; | ||
function isObject(obj){ | ||
@@ -26,2 +28,19 @@ return obj === Object(obj); | ||
function find(arr, findStr){ | ||
var findOptions = FIND_REGEX.exec(findStr); | ||
var left = findOptions[1].replace(/\|/g, '.'); | ||
var right = findOptions[3]; | ||
var operator = findOptions[2]; | ||
return arr.filter(function(a){ | ||
var value = deepRead(a, left); | ||
if(!value.found){ | ||
return false; | ||
} | ||
switch(operator){ | ||
case '=': return value.value === right; | ||
} | ||
}); | ||
} | ||
function deepRead(obj, path){ | ||
@@ -44,3 +63,5 @@ if(typeof obj !== 'object' || obj === null){ | ||
var prop = path[i]; | ||
if(obj && isSet(obj, prop)){ | ||
if(prop.match(FIND_REGEX)){ | ||
obj = find(obj, prop); | ||
}else if(obj && isSet(obj, prop)){ | ||
obj = obj[prop]; | ||
@@ -142,2 +163,20 @@ }else{ | ||
}, | ||
'pluckValues': { | ||
value: function(prop){ | ||
if(!result.found || !Array.isArray(result.value)){ | ||
return []; | ||
} | ||
var values = []; | ||
result.value.forEach(function(r){ | ||
var itemResult = deepRead(r, prop); | ||
if(itemResult.found){ | ||
values.push(itemResult.value); | ||
} | ||
}); | ||
return values; | ||
} | ||
}, | ||
'extract': { | ||
@@ -144,0 +183,0 @@ value: function(){ |
@@ -12,3 +12,4 @@ 'use strict'; | ||
esArticle: require('./fixtures/es-article.json'), | ||
esResults: require('./fixtures/es-results.json') | ||
esResults: require('./fixtures/es-results.json'), | ||
ontotext: require('./fixtures/ontotext-document.json') | ||
}; | ||
@@ -140,2 +141,9 @@ | ||
it('Should be able to pluck a single property and return an array of values', function() { | ||
var expected = fixtures.esResults.hits.hits.map(function(h){ return h._id}); | ||
var result = quizas(fixtures.esResults, 'hits.hits').pluckValues('_id'); | ||
expect(result).to.be.an('array'); | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
describe('Bugs', function() { | ||
@@ -142,0 +150,0 @@ |
980328
13
30790