tjmehta / keypather Goto Github PK
View Code? Open in Web Editor NEWGet, set, or delete deep object values using keypath strings (supports immutable operations) and more.
License: MIT License
Get, set, or delete deep object values using keypath strings (supports immutable operations) and more.
License: MIT License
Hi @tjmehta. TBH I was a little wary of using keypather. Looking at the impressive scope of functionality, I was sure you use eval
or new Function
or something of that kind for retrieving and setting the properties :)
I scanned the source and found out you don't. Wow! π Why not say it in the readme or somewhere? :)
var keypath = require('value-for-keypath')();
undefined
var obj = {};
undefined
keypath.set(obj, 'lms_step.lms_quiz._id', 'a');
'a'
obj
{ 'lms_step.lms_quiz': { _id: 'a' } }
immutable-set
immutable-del
Instead of "a.b.c"
keypather could accept an array like ["a", "b", "c"]
. As object-path does it.
Here's why.
{"[key]": "value"}
var keys = ["a", "[b]", "c()", "'d\''"];
β¦it seems very wrong to do:
keypather.set(...
, keys.map(function (key) {
return "['" + key.replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "']"
}).join("")
);
β¦when you could just:
keypather.set(..., keys);
keypather.flatten({ foo: {} }) // {}
I am trying to use this library but the limitation on characters that I can use makes it impossible to use for me.
I mean why it won't work if I have '/' or '{' in my keypath?
is it possible to give the developers an option to select their separator and then treat all other characters as normal characters?
I was trying to use the Library for parsing a folder structure having set of files and creating an object out of it.
I get error for a key which has hyphen in file name
if (!/^[A-Za-z0-9_$]$/.test(character)) throw new Error(parseErrorMessage(state, 'invalid dot key')) ^ Error: Error parsing keypath 'how-to' at char 4 '-': invalid dot key
> a = {}
> keypather.get(a, 'PROPERTY.THAT.DOESNT.EXIST')
null
> keypather.get(a, 'PROPERTY_THAT_DOESN_EXIST')
undefined
This seems like weird behavior to me. I would have expected either both to be null
or both to be undefined
.
I understand that it's probably a bit too late to change this, but thought I'd point it out. It seems to me that a value that doesn't exist should always return undefined
since it's... well... undefined, but I might be missing something.
I was trying to figure out the best way to make the code more modular/monad-like.
So, for example let's consider the path splitting features as chain-able modules: dottedPaths
, arrayPaths
, functionPaths
, or something like customPathMapper
This would make it easier to exclude, say, function-path features (XS exploits come to mind), not to mention it'd be easier to make custom keypather builds. Like a light-weight read-only path checker for the browser would be really handy, especially on mobile.
Anyway, I was just wondering what @tjmehta thoughts are on this.
Thanks for the awesome lib!
// mock example:
var getPath = require('keypather/get'); // note: get.js could be used by browserify to get only the dependencies required for reading objects
console.log('Email: ', getPath(user, 'contactInfo.email');
Branch | Build failing π¨ |
---|---|
Dependency | debug |
Current Version | 3.2.2 |
Type | dependency |
This version is covered by your current version range and after updating it in your project the build failed.
debug is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.
The new version differs by 1 commits.
700a010
re-introduce node.js (root file) (ref #603)
See the full diff
There is a collection of frequently asked questions. If those donβt help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot π΄
Hi,
keypath.expand
behavior is pretty weird with expanding an array.
The output of this:
keypath.expand(keypath.flatten({
notes: ["hello", "this"]
}));
is { "0": "hello", "1": "this" }
https://github.com/tjmehta/keypather/blob/master/index.js#L94
This check is unsafe.
typeof null === 'object'
Proposed solution, use Object.propertyIsEnumerable
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
Will send PR
import flatten from "keypather/flatten";
const myObj = { foo: [] }
const myOtherObj = { foo: ['bar'] }
flatten(myObj) // => {} Missing "foo" key
flatten(myOtherObj) // => { 'foo[0]': 'bar' }
π Hello, @tjmehta - @Nav-Prak has disclosed a potential vulnerability in your repository. To validate or invalidate this potential vulnerability, please visit https://huntr.dev/bounties/2-npm-keypather and join our community in helping secure open-source code.
Come and join us on our Discord and a member of our team will be happy to help! π€
cc - @JamieSlome
Hi, it would be great if import support is improved and documented.
Right now it's possible to use ES6 import with keypather this way, but it's not clean and you create a new instance every time you need it:
import { default as keypather } from 'keypather';
let myKeypather = keypather();
something like this would be better:
import keypather from 'keypather';
//if you need to set options
keypather.setOptions({...});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.