Created
December 7, 2017 00:56
-
-
Save jchip/966c913787092bb203d4b5df5f8e6d5a to your computer and use it in GitHub Desktop.
dirmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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