base-domain
Version:
simple module to help build Domain-Driven Design
100 lines (79 loc) • 2.27 kB
JavaScript
/**
utility functions
@class Util
@module base-domain
*/
var Util;
Util = (function() {
function Util() {}
/**
converts hyphenation to camel case
'shinout-no-macbook-pro' => 'ShinoutNoMacbookPro'
'shinout-no-macbook-pro' => 'shinoutNoMacbookPro' # if lowerFirst = true
@method camelize
@static
@param {String} hyphened
@param {Boolean} [lowerFirst=false] make capital char lower
@return {String} cameled
*/
Util.camelize = function(hyphened, lowerFirst) {
var i, substr;
if (lowerFirst == null) {
lowerFirst = false;
}
return ((function() {
var j, len, ref, results;
ref = hyphened.split('-');
results = [];
for (i = j = 0, len = ref.length; j < len; i = ++j) {
substr = ref[i];
if (i === 0 && lowerFirst) {
results.push(substr);
} else {
results.push(substr.charAt(0).toUpperCase() + substr.slice(1));
}
}
return results;
})()).join('');
};
/**
converts hyphenation to camel case
'ShinoutNoMacbookPro' => 'shinout-no-macbook-pro'
'ABC' => 'a-b-c' # current implementation... FIXME ?
@method hyphenize
@static
@param {String} hyphened
@return {String} cameled
*/
Util.hyphenize = function(cameled) {
cameled = cameled.charAt(0).toUpperCase() + cameled.slice(1);
return cameled.replace(/([A-Z])/g, function(st) {
return '-' + st.charAt(0).toLowerCase();
}).slice(1);
};
/**
requires js file
in Titanium, file-not-found-like-exception occurred in require function cannot be caught.
Thus, before require function is called, check the existence of the file.
File extension must be '.js' in Titanium.
@method requireFile
@static
@param {String} file name without extension
@return {any} required value
*/
Util.requireFile = function(file) {
var fileInfo, path;
if (typeof Ti === "undefined" || Ti === null) {
return require(file);
}
path = file + '.js';
fileInfo = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, path);
if (fileInfo.exists()) {
return require(file);
} else {
throw new Error(path + ": no such file.");
}
};
return Util;
})();
module.exports = Util;