ES6 + misc cleanup

This commit is contained in:
Bryan Ashby 2016-06-25 22:36:40 -06:00
parent aafa3b68eb
commit 8f1301d647
1 changed files with 35 additions and 47 deletions

View File

@ -1,10 +1,12 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
var Config = require('./config.js').config; // ENiGMA½
const Config = require('./config.js').config;
var _ = require('lodash'); // deps
var assert = require('assert'); const _ = require('lodash');
const assert = require('assert');
exports.parseAsset = parseAsset; exports.parseAsset = parseAsset;
exports.getAssetWithShorthand = getAssetWithShorthand; exports.getAssetWithShorthand = getAssetWithShorthand;
@ -17,19 +19,20 @@ const ALL_ASSETS = [
'art', 'art',
'menu', 'menu',
'method', 'method',
'module',
'systemMethod', 'systemMethod',
'systemModule', 'systemModule',
'prompt', 'prompt',
'config', 'config',
]; ];
var ASSET_RE = new RegExp('\\@(' + ALL_ASSETS.join('|') + ')\\:([\\w\\d\\.]*)(?:\\/([\\w\\d\\_]+))*'); const ASSET_RE = new RegExp('\\@(' + ALL_ASSETS.join('|') + ')\\:([\\w\\d\\.]*)(?:\\/([\\w\\d\\_]+))*');
function parseAsset(s) { function parseAsset(s) {
var m = ASSET_RE.exec(s); const m = ASSET_RE.exec(s);
if(m) { if(m) {
var result = { type : m[1] }; let result = { type : m[1] };
if(m[3]) { if(m[3]) {
result.location = m[2]; result.location = m[2];
@ -48,7 +51,7 @@ function getAssetWithShorthand(spec, defaultType) {
} }
if('@' === spec[0]) { if('@' === spec[0]) {
var asset = parseAsset(spec); const asset = parseAsset(spec);
assert(_.isString(asset.type)); assert(_.isString(asset.type));
return asset; return asset;
@ -56,63 +59,48 @@ function getAssetWithShorthand(spec, defaultType) {
return { return {
type : defaultType, type : defaultType,
asset : spec, asset : spec,
}
}
}
// :TODO: Convert these to getAssetWithShorthand()
function getArtAsset(art) {
if(!_.isString(art)) {
return null;
}
if('@' === art[0]) {
var artAsset = parseAsset(art);
assert('art' === artAsset.type || 'method' === artAsset.type);
return artAsset;
} else {
return {
type : 'art',
asset : art,
}; };
} }
} }
function getModuleAsset(module) { function getArtAsset(spec) {
if(!_.isString(module)) { const asset = getAssetWithShorthand(spec, 'art');
if(!asset) {
return null; return null;
} }
if('@' === module[0]) { assert( ['art', 'method' ].indexOf(asset.type) > -1);
var modAsset = parseAsset(module); return asset;
assert('module' === modAsset.type || 'systemModule' === modAsset.type);
return modAsset;
} else {
return {
type : 'module',
asset : module,
}
}
} }
function resolveConfigAsset(from) { function getModuleAsset(spec) {
var asset = parseAsset(from); const asset = getAssetWithShorthand(spec, 'module');
if(!asset) {
return null;
}
assert( ['module', 'systemModule' ].indexOf(asset.type) > -1);
return asset;
}
function resolveConfigAsset(spec) {
const asset = parseAsset(spec);
if(asset) { if(asset) {
assert('config' === asset.type); assert('config' === asset.type);
var path = asset.asset.split('.'); const path = asset.asset.split('.');
var conf = Config; let conf = Config;
for(var i = 0; i < path.length; ++i) { for(let i = 0; i < path.length; ++i) {
if(_.isUndefined(conf[path[i]])) { if(_.isUndefined(conf[path[i]])) {
return from; return spec;
} }
conf = conf[path[i]]; conf = conf[path[i]];
} }
return conf; return conf;
} else { } else {
return from; return spec;
} }
} }
@ -122,4 +110,4 @@ function getViewPropertyAsset(src) {
} }
return parseAsset(src); return parseAsset(src);
}; }