Skip to content

Instantly share code, notes, and snippets.

@jchip
Created December 7, 2017 00:56
Show Gist options
  • Save jchip/966c913787092bb203d4b5df5f8e6d5a to your computer and use it in GitHub Desktop.
Save jchip/966c913787092bb203d4b5df5f8e6d5a to your computer and use it in GitHub Desktop.
dirmap
class DirMap {
constructor() {
this._map = { _: {} };
}
set(dir, data) {
const splits = dir.split(path.sep);
let map = this._map;
let i;
for (i = 0; i < splits.length; i++) {
const x = splits[i];
if (!map._) {
map._ = {};
}
if (!map._[x]) {
map._[x] = {};
}
map = map._[x];
}
map.$ = data;
}
get(dir) {
const splits = dir.split(path.sep);
let map = this._map;
let i;
for (i = 0; i < splits.length && map; i++) {
const x = splits[i];
map = map._ && map._[x];
}
return map && map.$;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment