codecombat/app/lib/requireUtils.coffee

49 lines
1.2 KiB
CoffeeScript
Raw Normal View History

module.exports.getParentFolders = (subPath, urlPrefix='/test/') ->
return [] unless subPath
paths = []
parts = subPath.split('/')
while parts.length
parts.pop()
paths.unshift {
name: parts[parts.length-1] or 'All'
url: urlPrefix + parts.join('/')
}
paths
2014-06-30 22:16:26 -04:00
module.exports.parseImmediateChildren = (allChildren, subPath, baseRequirePath='test/app/', urlPrefix='/test/') ->
return [] unless allChildren
folders = {}
files = {}
requirePrefix = baseRequirePath + subPath
if requirePrefix[requirePrefix.length-1] isnt '/'
requirePrefix += '/'
for f in allChildren
f = f[requirePrefix.length..]
continue unless f
parts = f.split('/')
name = parts[0]
group = if parts.length is 1 then files else folders
group[name] ?= 0
group[name] += 1
children = []
urlPrefix += subPath
urlPrefix += '/' if urlPrefix[urlPrefix.length-1] isnt '/'
for name in _.keys(folders)
children.push {
2014-06-30 22:16:26 -04:00
type: 'folder',
url: urlPrefix+name
name: name+'/'
size: folders[name]
}
for name in _.keys(files)
children.push {
2014-06-30 22:16:26 -04:00
type: 'file',
url: urlPrefix+name
name: name
}
children