A Nodejs module to manipulate objects like a folder
$ npm install --save pathme
var pathme = require('pathme');
var myObj = { foo :{ bar : ['a','b','c'] }, pi : { ka: { chu : 'chuuuuuuuu' }} };
var pathObj = pathme(myObj);
or just simply
var pathObj = require('pathme')(myObj);
- move to a given attribute
pathObj.cd('/pi/ka/chu').cwd == 'chuuuuuuuu'
pathObj.cd('foor/bar/0').cwd == 'a'
//You can even play with tree-climbing:
pathObj.cd('foo/../pi/ka/../../pi/ka/../../pi/ka/chu').cwd == myObj.pi.ka.chu ;
pathObj.cd('/foo/bar/../../../../../../../../').cwd == myObj;
pathObj.cd('/').cwd == myObj;
//or chaining calls
pathObj.cd('pi').cd('ka').cd('chu').cwd == myObj.pi.ka.chu;
pathObj.cd('pi').cd('ka').cd('chu').cd('/') == myObj;
//Shell user would be familiar with pushd/popd commands
pathObj.pushd('/foo/bar').cwd == myObj.foo.bar; // move to foo/bar and save path to stack;
pathObj.pushd('/pi/ka').cwd == myObj.pi.ka; // move to /pi/ka and save path to stack;
pathObj.cd('/').push().cwd == myObj; // save current path to stack
pathObj.cd('/pi/ka/chu').cwd == myObj.pi.ka.chu;
pathObj.popd().cwd == myObj;
pathObj.popd().cwd == myObj.pi.ka;
pathObj.popd().cwd == myObj.foo.bar;
- test if a given attribute exists
//Testing from "root folder"
pathObj.exists('/pi/ka/chu') == true;
pathObj.exists('/pi/ka/chu/uuuuu') == false;
pathObj.exists('/pe/pe/ro/ni') == false;
//Or from a nested object
pathObj.cd('/foo').cd('bar').exists('p418') == false
From package root:
$> npm install
$> npm test
MIT © p418