Last active
August 29, 2015 14:20
-
-
Save shiawuen/57713b441ad8b231071e to your computer and use it in GitHub Desktop.
This file contains 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 FileSystem { | |
constructor () { | |
this.tree = {} | |
this.binaries = {} | |
} | |
add ({ type, name, path = '/'}) { | |
if (! (type && name)) | |
throw Error('missing required parameters') | |
if (!/\/$/.test(path)) path += '/' | |
this.tree[ path ] = { type, name, path: path + name } | |
} | |
move ({ origin, destination }) { | |
if (! (origin && destination)) | |
throw Error('missing required parameters') | |
origin = origin.replace(/\/$/, '') | |
destination = destination.replace(/\/$/, '') | |
let paths = [] | |
for (let p in this.tree) | |
if (-1 != p.indexOf(origin)) paths.push(p) | |
paths.forEach(path => { | |
const newPath = path.replace(origin, destination) | |
this.tree[ newPath ] = this.tree[ path ] | |
delete this.tree[ path ] | |
}) | |
} | |
write ({ path, content }) { | |
if (! (path && content)) | |
throw Error('missing required parameters') | |
this.binaries[ path ] = content | |
} | |
delete ({ path }) { | |
if (! path) | |
throw Error('missing required parameters') | |
let paths = [] | |
for (let p in this.tree) | |
if (-1 != p.indexOf(path)) paths.push(p) | |
paths.forEach(path => { | |
delete this.tree[ path ] | |
delete this.binaries[ path ] | |
}) | |
} | |
} | |
let fs = new FileSystem() | |
fs.add({ type: 'Drive', name: 'js' }) | |
fs.add({ type: 'Folder', name: 'jquery', path: '/js' }) | |
fs.move({ origin: '/js', destination: '/source' }) | |
fs.add({ type: 'File', name: 'index.js', path: '/js/jquery/' }) | |
fs.write({ path: '/js/jquery/index.js', content: 'console.log("hello world")' }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment