object-grep
Advanced tools
Comparing version 1.1.4 to 2.0.0
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.objectGrep=t():e.objectGrep=t()}(this,(function(){return(()=>{"use strict";var e={138:(e,t,o)=>{function r(e,t,o=100,n="",u={}){return 0===o||(t||(t=String(t)),Object.keys(e).forEach((p=>{const c=n?`${n}.${p}`:p,i=e[p];p.match(t)&&(u.keys||(u.keys=[]),u.keys.push(c)),["string","number","boolean","undefined"].includes(typeof i)||null===i?String(i).match(t)&&(u.values||(u.values=[]),u.values.push(c)):r(i,t,o-1,c,u)}))),u}o.r(t),o.d(t,{objectGrep:()=>r}),r.inject=function(){Object.defineProperty(Object.prototype,"grep",{enumerable:!1,value:function(e,t){return r(this,e,t)}})}}},t={};function o(r){if(t[r])return t[r].exports;var n=t[r]={exports:{}};return e[r](n,n.exports,o),n.exports}return o.d=(e,t)=>{for(var r in t)o.o(t,r)&&!o.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o(138)})()})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.objectGrep=t():e.objectGrep=t()}(this,(function(){return(()=>{"use strict";var e={138:(e,t,r)=>{function n({target:e,searchExpr:t,depth:r,path:o,result:u}){return r<1||Object.keys(e).forEach((i=>{const c=o?`${o}.${i}`:i,a=e[i];i.match(t)&&(u.inKeys[c]=a),["string","number","boolean","undefined"].includes(typeof a)||null===a?String(a).match(t)&&(u.inValues[c]=a):n({searchExpr:t,result:u,path:c,target:a,depth:r-1})})),u}function o(e,t,r=20){const o=Object.create({short:function(){return{inKeys:Object.keys(this.inKeys),inValues:Object.keys(this.values)}}},{inValues:{writable:!0,configurable:!0,enumerable:!0,value:{}},inKeys:{writable:!0,configurable:!0,enumerable:!0,value:{}}});return t||(t=String(t)),n({target:e,searchExpr:t,depth:r,path:"",result:o})}r.r(t),r.d(t,{objectGrep:()=>o}),o.inject=function(){Object.defineProperty(Object.prototype,"grep",{enumerable:!1,value:function(e,t){return o(this,e,t)}})}}},t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={exports:{}};return e[n](o,o.exports,r),o.exports}return r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r(138)})()})); |
{ | ||
"name": "object-grep", | ||
"version": "1.1.4", | ||
"version": "2.0.0", | ||
"description": "A javascript tool for searching text in keys and content inspired by linux grep", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -27,3 +27,3 @@ # object-grep | ||
```javascript | ||
const obj = { | ||
const target = { | ||
foo: { | ||
@@ -47,13 +47,46 @@ bar: { | ||
objectGrep(obj, 'baz') // => {keys: ['foo.bar.baz', 'foo.bar.baz.foo.bar.baz'], values: ['oof.rab.zab.2']} | ||
objectGrep(target, 'baz') // => {inKeys: {'foo.bar.baz': {foo: {…}}, 'foo.bar.baz.foo.bar.baz': 'zab'}, inValues: {'oof.rab.zab.2': 'baz'}} | ||
// or regexp | ||
objectGrep(obj, /b.z/) // => {keys: ['foo.bar.baz', 'foo.bar.baz.foo.bar.baz'], values: ['oof.rab.zab.2']} | ||
objectGrep(target, /b.z/) // => {inKeys: {'foo.bar.baz': {foo: {…}}, 'foo.bar.baz.foo.bar.baz': 'zab'}, inValues: {'oof.rab.zab.2': 'baz'}} | ||
// or with depth limit | ||
objectGrep(obj, /b.z/, 4) // => {keys: ['foo.bar.baz'], values: ['oof.rab.zab.2']} | ||
objectGrep(target, /b.z/, 4) // => {inKeys: {'foo.bar.baz': {foo: {…} }}, inValues: {'oof.rab.zab.2': 'baz'}} | ||
``` | ||
## short view | ||
You can also use a short output format. To do this, call the `short()` method on the result. This way you will only see paths to keys and values with no data stored on those paths | ||
```javascript | ||
const target = { | ||
foo: { | ||
bar: { | ||
baz: { | ||
foo: { | ||
bar: { | ||
baz: 'zab' | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
oof: { | ||
rab: { | ||
zab: ['foo', 'bar', 'baz', 'zab', 'rab', 'oof'] | ||
} | ||
} | ||
} | ||
objectGrep(target, 'baz').short() // => {inKeys: ['foo.bar.baz', 'foo.bar.baz.foo.bar.baz'], inValues: ['oof.rab.zab.2']} | ||
// or regexp | ||
objectGrep(target, /b.z/).short() // => {inKeys: ['foo.bar.baz', 'foo.bar.baz.foo.bar.baz'], inValues: ['oof.rab.zab.2']} | ||
// or with depth limit | ||
objectGrep(target, /b.z/, 4).short() // => {inKeys: ['foo.bar.baz'], inValues: ['oof.rab.zab.2']} | ||
``` | ||
## inject | ||
@@ -66,5 +99,5 @@ | ||
const obj = { a: { b : { c: 'd' } } } | ||
const target = { a: { b : { c: 'd' } } } | ||
obj.grep('b') // => {keys: ['a.b']} | ||
target.grep('b') // => {keys: ['a.b']} | ||
``` | ||
@@ -71,0 +104,0 @@ |
@@ -1,20 +0,12 @@ | ||
export function objectGrep(obj, searchExpr, depth = 100, path = '', result = {}) { | ||
if (depth === 0) { | ||
function grep({target, searchExpr, depth, path, result}) { | ||
if (depth < 1) { | ||
return result; | ||
} | ||
if (!searchExpr) { | ||
searchExpr = String(searchExpr); | ||
} | ||
Object.keys(obj).forEach((key) => { | ||
Object.keys(target).forEach((key) => { | ||
const keyPath = !path ? key : `${path}.${key}`; | ||
const value = obj[key]; | ||
const value = target[key]; | ||
if (key.match(searchExpr)) { | ||
if (!result.keys) { | ||
result.keys = []; | ||
} | ||
result.keys.push(keyPath); | ||
result.inKeys[keyPath] = value; | ||
} | ||
@@ -24,7 +16,3 @@ | ||
if (String(value).match(searchExpr)) { | ||
if (!result.values) { | ||
result.values = []; | ||
} | ||
result.values.push(keyPath); | ||
result.inValues[keyPath] = value; | ||
} | ||
@@ -35,3 +23,9 @@ | ||
objectGrep(value, searchExpr, depth - 1, keyPath, result); | ||
grep({ | ||
searchExpr, | ||
result, | ||
path: keyPath, | ||
target: value, | ||
depth: depth - 1, | ||
}); | ||
}); | ||
@@ -42,2 +36,32 @@ | ||
export function objectGrep(target, searchExpr, depth = 20) { | ||
const result = Object.create({ | ||
short: function () { | ||
return { | ||
inKeys: Object.keys(this.inKeys), | ||
inValues: Object.keys(this.values), | ||
}; | ||
} | ||
}, | ||
{ | ||
inValues: {writable: true, configurable: true, enumerable: true, value: {}}, | ||
inKeys: {writable: true, configurable: true, enumerable: true, value: {}} | ||
} | ||
); | ||
const path = ''; | ||
if (!searchExpr) { | ||
searchExpr = String(searchExpr); | ||
} | ||
return grep({ | ||
target, | ||
searchExpr, | ||
depth, | ||
path, | ||
result | ||
}); | ||
} | ||
objectGrep.inject = function () { | ||
@@ -44,0 +68,0 @@ Object.defineProperty(Object.prototype, 'grep', { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7998
60
106