atma-utils
Advanced tools
Comparing version 0.1.23 to 0.1.24
1018
lib/utils.js
@@ -42,511 +42,2 @@ (function(factory){ | ||
}()); | ||
var class_Dfr; | ||
(function(){ | ||
//@TODO remove constructr run | ||
class_Dfr = function (mix) { | ||
if (typeof mix === 'function') { | ||
return class_Dfr.run(mix); | ||
} | ||
}; | ||
class_Dfr.prototype = { | ||
_isAsync: true, | ||
_done: null, | ||
_fail: null, | ||
_always: null, | ||
_resolved: null, | ||
_rejected: null, | ||
defer: function () { | ||
this._rejected = null; | ||
this._resolved = null; | ||
return this; | ||
}, | ||
isResolved: function () { | ||
return this._resolved != null; | ||
}, | ||
isRejected: function () { | ||
return this._rejected != null; | ||
}, | ||
isBusy: function () { | ||
return this._resolved == null && this._rejected == null; | ||
}, | ||
resolve: function () { | ||
var done = this._done, always = this._always; | ||
this._resolved = arguments; | ||
dfr_clearListeners(this); | ||
arr_callOnce(done, this, arguments); | ||
arr_callOnce(always, this, [this]); | ||
return this; | ||
}, | ||
reject: function () { | ||
var fail = this._fail, always = this._always; | ||
this._rejected = arguments; | ||
dfr_clearListeners(this); | ||
arr_callOnce(fail, this, arguments); | ||
arr_callOnce(always, this, [this]); | ||
return this; | ||
}, | ||
then: function (filterSuccess, filterError) { | ||
return this.pipe(filterSuccess, filterError); | ||
}, | ||
done: function (callback) { | ||
if (this._rejected != null) | ||
return this; | ||
return dfr_bind(this, this._resolved, this._done || (this._done = []), callback); | ||
}, | ||
fail: function (callback) { | ||
if (this._resolved != null) | ||
return this; | ||
return dfr_bind(this, this._rejected, this._fail || (this._fail = []), callback); | ||
}, | ||
always: function (callback) { | ||
return dfr_bind(this, this._rejected || this._resolved, this._always || (this._always = []), callback); | ||
}, | ||
pipe: function (mix /* ..methods */) { | ||
var dfr; | ||
if (typeof mix === 'function') { | ||
dfr = new class_Dfr(); | ||
var done_ = mix, fail_ = arguments.length > 1 | ||
? arguments[1] | ||
: null; | ||
this | ||
.done(delegate(dfr, 'resolve', done_)) | ||
.fail(delegate(dfr, 'reject', fail_)); | ||
return dfr; | ||
} | ||
dfr = mix; | ||
var imax = arguments.length, done = imax === 1, fail = imax === 1, i = 0, x; | ||
while (++i < imax) { | ||
x = arguments[i]; | ||
switch (x) { | ||
case 'done': | ||
done = true; | ||
break; | ||
case 'fail': | ||
fail = true; | ||
break; | ||
default: | ||
console.error('Unsupported pipe channel', arguments[i]); | ||
break; | ||
} | ||
} | ||
done && this.done(delegate(dfr, 'resolve')); | ||
fail && this.fail(delegate(dfr, 'reject')); | ||
function pipe(dfr, method) { | ||
return function () { | ||
dfr[method].apply(dfr, arguments); | ||
}; | ||
} | ||
function delegate(dfr, name, fn) { | ||
return function () { | ||
if (fn != null) { | ||
var override = fn.apply(this, arguments); | ||
if (override != null && override !== dfr) { | ||
if (isDeferred(override)) { | ||
override.then(delegate(dfr, 'resolve'), delegate(dfr, 'reject')); | ||
return; | ||
} | ||
dfr[name](override); | ||
return; | ||
} | ||
} | ||
dfr[name].apply(dfr, arguments); | ||
}; | ||
} | ||
return this; | ||
}, | ||
pipeCallback: function () { | ||
var self = this; | ||
return function (error) { | ||
if (error != null) { | ||
self.reject(error); | ||
return; | ||
} | ||
var args = _Array_slice.call(arguments, 1); | ||
fn_apply(self.resolve, self, args); | ||
}; | ||
}, | ||
resolveDelegate: function () { | ||
return fn_proxy(this.resolve, this); | ||
}, | ||
rejectDelegate: function () { | ||
return fn_proxy(this.reject, this); | ||
} | ||
}; | ||
class_Dfr.resolve = function (a, b, c) { | ||
var dfr = new class_Dfr(); | ||
return dfr.resolve.apply(dfr, _Array_slice.call(arguments)); | ||
}; | ||
class_Dfr.reject = function (error) { | ||
var dfr = new class_Dfr(); | ||
return dfr.reject(error); | ||
}; | ||
class_Dfr.run = function (fn, ctx) { | ||
var dfr = new class_Dfr(); | ||
if (ctx == null) | ||
ctx = dfr; | ||
fn.call(ctx, fn_proxy(dfr.resolve, ctx), fn_proxy(dfr.reject, dfr), dfr); | ||
return dfr; | ||
}; | ||
class_Dfr.all = function (promises) { | ||
var dfr = new class_Dfr, arr = new Array(promises.length), wait = promises.length, error = null; | ||
if (wait === 0) { | ||
return dfr.resolve(arr); | ||
} | ||
function tick(index) { | ||
if (error != null) { | ||
return; | ||
} | ||
var args = _Array_slice.call(arguments, 1); | ||
arr.splice.apply(arr, [index, 0].concat(args)); | ||
if (--wait === 0) { | ||
dfr.resolve(arr); | ||
} | ||
} | ||
function onReject(err) { | ||
dfr.reject(error = err); | ||
} | ||
var imax = promises.length, i = -1; | ||
while (++i < imax) { | ||
var x = promises[i]; | ||
if (x == null || x.then == null) { | ||
tick(i); | ||
continue; | ||
} | ||
x.then(tick.bind(null, i), onReject); | ||
} | ||
return dfr; | ||
}; | ||
// PRIVATE | ||
function dfr_bind(dfr, arguments_, listeners, callback) { | ||
if (callback == null) | ||
return dfr; | ||
if (arguments_ != null) | ||
fn_apply(callback, dfr, arguments_); | ||
else | ||
listeners.push(callback); | ||
return dfr; | ||
} | ||
function dfr_clearListeners(dfr) { | ||
dfr._done = null; | ||
dfr._fail = null; | ||
dfr._always = null; | ||
} | ||
function arr_callOnce(arr, ctx, args) { | ||
if (arr == null) | ||
return; | ||
var imax = arr.length, i = -1, fn; | ||
while (++i < imax) { | ||
fn = arr[i]; | ||
if (fn) | ||
fn_apply(fn, ctx, args); | ||
} | ||
arr.length = 0; | ||
} | ||
function isDeferred(x) { | ||
return x != null | ||
&& typeof x === 'object' | ||
&& is_Function(x.then); | ||
} | ||
}()); | ||
var class_Uri; | ||
(function(){ | ||
class_Uri = class_create({ | ||
protocol: null, | ||
value: null, | ||
path: null, | ||
file: null, | ||
extension: null, | ||
constructor: function (uri) { | ||
if (uri == null) | ||
return this; | ||
if (util_isUri(uri)) | ||
return uri.combine(''); | ||
uri = normalize_uri(uri); | ||
this.value = uri; | ||
parse_protocol(this); | ||
parse_host(this); | ||
parse_search(this); | ||
parse_file(this); | ||
// normilize path - "/some/path" | ||
this.path = normalize_pathsSlashes(this.value); | ||
if (/^[\w]+:\//.test(this.path)) { | ||
this.path = '/' + this.path; | ||
} | ||
return this; | ||
}, | ||
cdUp: function () { | ||
var path = this.path; | ||
if (path == null || path === '' || path === '/') { | ||
return this; | ||
} | ||
// win32 - is base drive | ||
if (/^\/?[a-zA-Z]+:\/?$/.test(path)) { | ||
return this; | ||
} | ||
this.path = path.replace(/\/?[^\/]+\/?$/i, ''); | ||
return this; | ||
}, | ||
/** | ||
* '/path' - relative to host | ||
* '../path', 'path','./path' - relative to current path | ||
*/ | ||
combine: function (path) { | ||
if (util_isUri(path)) { | ||
path = path.toString(); | ||
} | ||
if (!path) { | ||
return util_clone(this); | ||
} | ||
if (rgx_win32Drive.test(path)) { | ||
return new class_Uri(path); | ||
} | ||
var uri = util_clone(this); | ||
uri.value = path; | ||
parse_search(uri); | ||
parse_file(uri); | ||
if (!uri.value) { | ||
return uri; | ||
} | ||
path = uri.value.replace(/^\.\//i, ''); | ||
if (path[0] === '/') { | ||
uri.path = path; | ||
return uri; | ||
} | ||
while (/^(\.\.\/?)/ig.test(path)) { | ||
uri.cdUp(); | ||
path = path.substring(3); | ||
} | ||
uri.path = normalize_pathsSlashes(util_combinePathes(uri.path, path)); | ||
return uri; | ||
}, | ||
toString: function () { | ||
var protocol = this.protocol ? this.protocol + '://' : ''; | ||
var path = util_combinePathes(this.host, this.path, this.file) + (this.search || ''); | ||
var str = protocol + path; | ||
if (!(this.file || this.search) && this.path) { | ||
str += '/'; | ||
} | ||
return str; | ||
}, | ||
toPathAndQuery: function () { | ||
return util_combinePathes(this.path, this.file) + (this.search || ''); | ||
}, | ||
/** | ||
* @return Current Uri Path{String} that is relative to @arg1 Uri | ||
*/ | ||
toRelativeString: function (uri) { | ||
if (typeof uri === 'string') | ||
uri = new class_Uri(uri); | ||
if (this.path.indexOf(uri.path) === 0) { | ||
// host folder | ||
var p = this.path ? this.path.replace(uri.path, '') : ''; | ||
if (p[0] === '/') | ||
p = p.substring(1); | ||
return util_combinePathes(p, this.file) + (this.search || ''); | ||
} | ||
// sub folder | ||
var current = this.path.split('/'), relative = uri.path.split('/'), commonpath = '', i = 0, length = Math.min(current.length, relative.length); | ||
for (; i < length; i++) { | ||
if (current[i] === relative[i]) | ||
continue; | ||
break; | ||
} | ||
if (i > 0) | ||
commonpath = current.splice(0, i).join('/'); | ||
if (commonpath) { | ||
var sub = '', path = uri.path, forward; | ||
while (path) { | ||
if (this.path.indexOf(path) === 0) { | ||
forward = this.path.replace(path, ''); | ||
break; | ||
} | ||
path = path.replace(/\/?[^\/]+\/?$/i, ''); | ||
sub += '../'; | ||
} | ||
return util_combinePathes(sub, forward, this.file); | ||
} | ||
return this.toString(); | ||
}, | ||
toLocalFile: function () { | ||
var path = util_combinePathes(this.host, this.path, this.file); | ||
return util_win32Path(path); | ||
}, | ||
toLocalDir: function () { | ||
var path = util_combinePathes(this.host, this.path, '/'); | ||
return util_win32Path(path); | ||
}, | ||
toDir: function () { | ||
var str = this.protocol ? this.protocol + '://' : ''; | ||
return str + util_combinePathes(this.host, this.path, '/'); | ||
}, | ||
isRelative: function () { | ||
return !(this.protocol || this.host); | ||
}, | ||
getName: function () { | ||
return this.file.replace('.' + this.extension, ''); | ||
} | ||
}); | ||
var rgx_protocol = /^([\w\d]+):\/\//, rgx_extension = /\.([\w\d]+)$/i, rgx_win32Drive = /(^\/?\w{1}:)(\/|$)/, rgx_fileWithExt = /([^\/]+(\.[\w\d]+)?)$/i; | ||
function util_isUri(object) { | ||
return object && typeof object === 'object' && typeof object.combine === 'function'; | ||
} | ||
function util_combinePathes(a, b, c, d) { | ||
var args = arguments, str = ''; | ||
for (var i = 0, x, imax = arguments.length; i < imax; i++) { | ||
x = arguments[i]; | ||
if (!x) | ||
continue; | ||
if (!str) { | ||
str = x; | ||
continue; | ||
} | ||
if (str[str.length - 1] !== '/') | ||
str += '/'; | ||
str += x[0] === '/' ? x.substring(1) : x; | ||
} | ||
return str; | ||
} | ||
function normalize_pathsSlashes(str) { | ||
if (str[str.length - 1] === '/') { | ||
return str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
} | ||
function util_clone(source) { | ||
var uri = new class_Uri(), key; | ||
for (key in source) { | ||
if (typeof source[key] === 'string') { | ||
uri[key] = source[key]; | ||
} | ||
} | ||
return uri; | ||
} | ||
function normalize_uri(str) { | ||
return str | ||
.replace(/\\/g, '/') | ||
.replace(/^\.\//, '') | ||
// win32 drive path | ||
.replace(/^(\w+):\/([^\/])/, '/$1:/$2'); | ||
} | ||
function util_win32Path(path) { | ||
if (rgx_win32Drive.test(path) && path[0] === '/') { | ||
return path.substring(1); | ||
} | ||
return path; | ||
} | ||
function parse_protocol(obj) { | ||
var match = rgx_protocol.exec(obj.value); | ||
if (match == null && obj.value[0] === '/') { | ||
obj.protocol = 'file'; | ||
} | ||
if (match == null) | ||
return; | ||
obj.protocol = match[1]; | ||
obj.value = obj.value.substring(match[0].length); | ||
} | ||
function parse_host(obj) { | ||
if (obj.protocol == null) | ||
return; | ||
if (obj.protocol === 'file') { | ||
var match = rgx_win32Drive.exec(obj.value); | ||
if (match) { | ||
obj.host = match[1]; | ||
obj.value = obj.value.substring(obj.host.length); | ||
} | ||
return; | ||
} | ||
var pathStart = obj.value.indexOf('/', 2); | ||
obj.host = ~pathStart | ||
? obj.value.substring(0, pathStart) | ||
: obj.value; | ||
obj.value = obj.value.replace(obj.host, ''); | ||
} | ||
function parse_search(obj) { | ||
var question = obj.value.indexOf('?'); | ||
if (question === -1) | ||
return; | ||
obj.search = obj.value.substring(question); | ||
obj.value = obj.value.substring(0, question); | ||
} | ||
function parse_file(obj) { | ||
var match = rgx_fileWithExt.exec(obj.value), file = match == null ? null : match[1]; | ||
if (file == null) { | ||
return; | ||
} | ||
obj.file = file; | ||
obj.value = obj.value.substring(0, obj.value.length - file.length); | ||
obj.value = normalize_pathsSlashes(obj.value); | ||
match = rgx_extension.exec(file); | ||
obj.extension = match == null ? null : match[1]; | ||
} | ||
class_Uri.combinePathes = util_combinePathes; | ||
class_Uri.combine = util_combinePathes; | ||
}()); | ||
var class_EventEmitter; | ||
(function(){ | ||
class_EventEmitter = function () { | ||
this._listeners = {}; | ||
}; | ||
class_EventEmitter.prototype = { | ||
on: function (event, fn) { | ||
if (fn != null) { | ||
(this._listeners[event] || (this._listeners[event] = [])).push(fn); | ||
} | ||
return this; | ||
}, | ||
once: function (event, fn) { | ||
if (fn != null) { | ||
fn._once = true; | ||
(this._listeners[event] || (this._listeners[event] = [])).push(fn); | ||
} | ||
return this; | ||
}, | ||
pipe: function (event) { | ||
var that = this, args; | ||
return function () { | ||
args = _Array_slice.call(arguments); | ||
args.unshift(event); | ||
fn_apply(that.trigger, that, args); | ||
}; | ||
}, | ||
emit: event_trigger, | ||
trigger: event_trigger, | ||
off: function (event, fn) { | ||
var listeners = this._listeners[event]; | ||
if (listeners == null) | ||
return this; | ||
if (arguments.length === 1) { | ||
listeners.length = 0; | ||
return this; | ||
} | ||
var imax = listeners.length, i = -1; | ||
while (++i < imax) { | ||
if (listeners[i] === fn) { | ||
listeners.splice(i, 1); | ||
i--; | ||
imax--; | ||
} | ||
} | ||
return this; | ||
} | ||
}; | ||
function event_trigger() { | ||
var args = _Array_slice.call(arguments), event = args.shift(), fns = this._listeners[event], fn, imax, i = 0; | ||
if (fns == null) | ||
return this; | ||
for (imax = fns.length; i < imax; i++) { | ||
fn = fns[i]; | ||
fn_apply(fn, this, args); | ||
if (fn._once === true) { | ||
fns.splice(i, 1); | ||
i--; | ||
imax--; | ||
} | ||
} | ||
return this; | ||
} | ||
}()); | ||
var class_create, | ||
@@ -1069,2 +560,511 @@ class_createEx; | ||
}()); | ||
var class_Dfr; | ||
(function(){ | ||
//@TODO remove constructr run | ||
class_Dfr = function (mix) { | ||
if (typeof mix === 'function') { | ||
return class_Dfr.run(mix); | ||
} | ||
}; | ||
class_Dfr.prototype = { | ||
_isAsync: true, | ||
_done: null, | ||
_fail: null, | ||
_always: null, | ||
_resolved: null, | ||
_rejected: null, | ||
defer: function () { | ||
this._rejected = null; | ||
this._resolved = null; | ||
return this; | ||
}, | ||
isResolved: function () { | ||
return this._resolved != null; | ||
}, | ||
isRejected: function () { | ||
return this._rejected != null; | ||
}, | ||
isBusy: function () { | ||
return this._resolved == null && this._rejected == null; | ||
}, | ||
resolve: function () { | ||
var done = this._done, always = this._always; | ||
this._resolved = arguments; | ||
dfr_clearListeners(this); | ||
arr_callOnce(done, this, arguments); | ||
arr_callOnce(always, this, [this]); | ||
return this; | ||
}, | ||
reject: function () { | ||
var fail = this._fail, always = this._always; | ||
this._rejected = arguments; | ||
dfr_clearListeners(this); | ||
arr_callOnce(fail, this, arguments); | ||
arr_callOnce(always, this, [this]); | ||
return this; | ||
}, | ||
then: function (filterSuccess, filterError) { | ||
return this.pipe(filterSuccess, filterError); | ||
}, | ||
done: function (callback) { | ||
if (this._rejected != null) | ||
return this; | ||
return dfr_bind(this, this._resolved, this._done || (this._done = []), callback); | ||
}, | ||
fail: function (callback) { | ||
if (this._resolved != null) | ||
return this; | ||
return dfr_bind(this, this._rejected, this._fail || (this._fail = []), callback); | ||
}, | ||
always: function (callback) { | ||
return dfr_bind(this, this._rejected || this._resolved, this._always || (this._always = []), callback); | ||
}, | ||
pipe: function (mix /* ..methods */) { | ||
var dfr; | ||
if (typeof mix === 'function') { | ||
dfr = new class_Dfr(); | ||
var done_ = mix, fail_ = arguments.length > 1 | ||
? arguments[1] | ||
: null; | ||
this | ||
.done(delegate(dfr, 'resolve', done_)) | ||
.fail(delegate(dfr, 'reject', fail_)); | ||
return dfr; | ||
} | ||
dfr = mix; | ||
var imax = arguments.length, done = imax === 1, fail = imax === 1, i = 0, x; | ||
while (++i < imax) { | ||
x = arguments[i]; | ||
switch (x) { | ||
case 'done': | ||
done = true; | ||
break; | ||
case 'fail': | ||
fail = true; | ||
break; | ||
default: | ||
console.error('Unsupported pipe channel', arguments[i]); | ||
break; | ||
} | ||
} | ||
done && this.done(delegate(dfr, 'resolve')); | ||
fail && this.fail(delegate(dfr, 'reject')); | ||
function pipe(dfr, method) { | ||
return function () { | ||
dfr[method].apply(dfr, arguments); | ||
}; | ||
} | ||
function delegate(dfr, name, fn) { | ||
return function () { | ||
if (fn != null) { | ||
var override = fn.apply(this, arguments); | ||
if (override != null && override !== dfr) { | ||
if (isDeferred(override)) { | ||
override.then(delegate(dfr, 'resolve'), delegate(dfr, 'reject')); | ||
return; | ||
} | ||
dfr[name](override); | ||
return; | ||
} | ||
} | ||
dfr[name].apply(dfr, arguments); | ||
}; | ||
} | ||
return this; | ||
}, | ||
pipeCallback: function () { | ||
var self = this; | ||
return function (error) { | ||
if (error != null) { | ||
self.reject(error); | ||
return; | ||
} | ||
var args = _Array_slice.call(arguments, 1); | ||
fn_apply(self.resolve, self, args); | ||
}; | ||
}, | ||
resolveDelegate: function () { | ||
return fn_proxy(this.resolve, this); | ||
}, | ||
rejectDelegate: function () { | ||
return fn_proxy(this.reject, this); | ||
} | ||
}; | ||
class_Dfr.resolve = function (a, b, c) { | ||
var dfr = new class_Dfr(); | ||
return dfr.resolve.apply(dfr, _Array_slice.call(arguments)); | ||
}; | ||
class_Dfr.reject = function (error) { | ||
var dfr = new class_Dfr(); | ||
return dfr.reject(error); | ||
}; | ||
class_Dfr.run = function (fn, ctx) { | ||
var dfr = new class_Dfr(); | ||
if (ctx == null) | ||
ctx = dfr; | ||
fn.call(ctx, fn_proxy(dfr.resolve, ctx), fn_proxy(dfr.reject, dfr), dfr); | ||
return dfr; | ||
}; | ||
class_Dfr.all = function (promises) { | ||
var dfr = new class_Dfr, arr = new Array(promises.length), wait = promises.length, error = null; | ||
if (wait === 0) { | ||
return dfr.resolve(arr); | ||
} | ||
function tick(index) { | ||
if (error != null) { | ||
return; | ||
} | ||
var args = _Array_slice.call(arguments, 1); | ||
arr.splice.apply(arr, [index, 0].concat(args)); | ||
if (--wait === 0) { | ||
dfr.resolve(arr); | ||
} | ||
} | ||
function onReject(err) { | ||
dfr.reject(error = err); | ||
} | ||
var imax = promises.length, i = -1; | ||
while (++i < imax) { | ||
var x = promises[i]; | ||
if (x == null || x.then == null) { | ||
tick(i); | ||
continue; | ||
} | ||
x.then(tick.bind(null, i), onReject); | ||
} | ||
return dfr; | ||
}; | ||
// PRIVATE | ||
function dfr_bind(dfr, arguments_, listeners, callback) { | ||
if (callback == null) | ||
return dfr; | ||
if (arguments_ != null) | ||
fn_apply(callback, dfr, arguments_); | ||
else | ||
listeners.push(callback); | ||
return dfr; | ||
} | ||
function dfr_clearListeners(dfr) { | ||
dfr._done = null; | ||
dfr._fail = null; | ||
dfr._always = null; | ||
} | ||
function arr_callOnce(arr, ctx, args) { | ||
if (arr == null) | ||
return; | ||
var imax = arr.length, i = -1, fn; | ||
while (++i < imax) { | ||
fn = arr[i]; | ||
if (fn) | ||
fn_apply(fn, ctx, args); | ||
} | ||
arr.length = 0; | ||
} | ||
function isDeferred(x) { | ||
return x != null | ||
&& typeof x === 'object' | ||
&& is_Function(x.then); | ||
} | ||
}()); | ||
var class_Uri; | ||
(function(){ | ||
class_Uri = class_create({ | ||
protocol: null, | ||
value: null, | ||
path: null, | ||
file: null, | ||
extension: null, | ||
constructor: function (uri) { | ||
if (uri == null) | ||
return this; | ||
if (util_isUri(uri)) | ||
return uri.combine(''); | ||
uri = normalize_uri(uri); | ||
this.value = uri; | ||
parse_protocol(this); | ||
parse_host(this); | ||
parse_search(this); | ||
parse_file(this); | ||
// normilize path - "/some/path" | ||
this.path = normalize_pathsSlashes(this.value); | ||
if (/^[\w]+:\//.test(this.path)) { | ||
this.path = '/' + this.path; | ||
} | ||
return this; | ||
}, | ||
cdUp: function () { | ||
var path = this.path; | ||
if (path == null || path === '' || path === '/') { | ||
return this; | ||
} | ||
// win32 - is base drive | ||
if (/^\/?[a-zA-Z]+:\/?$/.test(path)) { | ||
return this; | ||
} | ||
this.path = path.replace(/\/?[^\/]+\/?$/i, ''); | ||
return this; | ||
}, | ||
/** | ||
* '/path' - relative to host | ||
* '../path', 'path','./path' - relative to current path | ||
*/ | ||
combine: function (path) { | ||
if (util_isUri(path)) { | ||
path = path.toString(); | ||
} | ||
if (!path) { | ||
return util_clone(this); | ||
} | ||
if (rgx_win32Drive.test(path)) { | ||
return new class_Uri(path); | ||
} | ||
var uri = util_clone(this); | ||
uri.value = path; | ||
parse_search(uri); | ||
parse_file(uri); | ||
if (!uri.value) { | ||
return uri; | ||
} | ||
path = uri.value.replace(/^\.\//i, ''); | ||
if (path[0] === '/') { | ||
uri.path = path; | ||
return uri; | ||
} | ||
while (/^(\.\.\/?)/ig.test(path)) { | ||
uri.cdUp(); | ||
path = path.substring(3); | ||
} | ||
uri.path = normalize_pathsSlashes(util_combinePathes(uri.path, path)); | ||
return uri; | ||
}, | ||
toString: function () { | ||
var protocol = this.protocol ? this.protocol + '://' : ''; | ||
var path = util_combinePathes(this.host, this.path, this.file) + (this.search || ''); | ||
var str = protocol + path; | ||
if (!(this.file || this.search) && this.path) { | ||
str += '/'; | ||
} | ||
return str; | ||
}, | ||
toPathAndQuery: function () { | ||
return util_combinePathes(this.path, this.file) + (this.search || ''); | ||
}, | ||
/** | ||
* @return Current Uri Path{String} that is relative to @arg1 Uri | ||
*/ | ||
toRelativeString: function (uri) { | ||
if (typeof uri === 'string') | ||
uri = new class_Uri(uri); | ||
if (this.path.indexOf(uri.path) === 0) { | ||
// host folder | ||
var p = this.path ? this.path.replace(uri.path, '') : ''; | ||
if (p[0] === '/') | ||
p = p.substring(1); | ||
return util_combinePathes(p, this.file) + (this.search || ''); | ||
} | ||
// sub folder | ||
var current = this.path.split('/'), relative = uri.path.split('/'), commonpath = '', i = 0, length = Math.min(current.length, relative.length); | ||
for (; i < length; i++) { | ||
if (current[i] === relative[i]) | ||
continue; | ||
break; | ||
} | ||
if (i > 0) | ||
commonpath = current.splice(0, i).join('/'); | ||
if (commonpath) { | ||
var sub = '', path = uri.path, forward; | ||
while (path) { | ||
if (this.path.indexOf(path) === 0) { | ||
forward = this.path.replace(path, ''); | ||
break; | ||
} | ||
path = path.replace(/\/?[^\/]+\/?$/i, ''); | ||
sub += '../'; | ||
} | ||
return util_combinePathes(sub, forward, this.file); | ||
} | ||
return this.toString(); | ||
}, | ||
toLocalFile: function () { | ||
var path = util_combinePathes(this.host, this.path, this.file); | ||
return util_win32Path(path); | ||
}, | ||
toLocalDir: function () { | ||
var path = util_combinePathes(this.host, this.path, '/'); | ||
return util_win32Path(path); | ||
}, | ||
toDir: function () { | ||
var str = this.protocol ? this.protocol + '://' : ''; | ||
return str + util_combinePathes(this.host, this.path, '/'); | ||
}, | ||
isRelative: function () { | ||
return !(this.protocol || this.host); | ||
}, | ||
getName: function () { | ||
return this.file.replace('.' + this.extension, ''); | ||
} | ||
}); | ||
var rgx_protocol = /^([\w\d]+):\/\//, rgx_extension = /\.([\w\d]+)$/i, rgx_win32Drive = /(^\/?\w{1}:)(\/|$)/, rgx_fileWithExt = /([^\/]+(\.[\w\d]+)?)$/i; | ||
function util_isUri(object) { | ||
return object && typeof object === 'object' && typeof object.combine === 'function'; | ||
} | ||
function util_combinePathes(a, b, c, d) { | ||
var args = arguments, str = ''; | ||
for (var i = 0, x, imax = arguments.length; i < imax; i++) { | ||
x = arguments[i]; | ||
if (!x) | ||
continue; | ||
if (!str) { | ||
str = x; | ||
continue; | ||
} | ||
if (str[str.length - 1] !== '/') | ||
str += '/'; | ||
str += x[0] === '/' ? x.substring(1) : x; | ||
} | ||
return str; | ||
} | ||
function normalize_pathsSlashes(str) { | ||
if (str[str.length - 1] === '/') { | ||
return str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
} | ||
function util_clone(source) { | ||
var uri = new class_Uri(), key; | ||
for (key in source) { | ||
if (typeof source[key] === 'string') { | ||
uri[key] = source[key]; | ||
} | ||
} | ||
return uri; | ||
} | ||
function normalize_uri(str) { | ||
return str | ||
.replace(/\\/g, '/') | ||
.replace(/^\.\//, '') | ||
// win32 drive path | ||
.replace(/^(\w+):\/([^\/])/, '/$1:/$2'); | ||
} | ||
function util_win32Path(path) { | ||
if (rgx_win32Drive.test(path) && path[0] === '/') { | ||
return path.substring(1); | ||
} | ||
return path; | ||
} | ||
function parse_protocol(obj) { | ||
var match = rgx_protocol.exec(obj.value); | ||
if (match == null && obj.value[0] === '/') { | ||
obj.protocol = 'file'; | ||
} | ||
if (match == null) | ||
return; | ||
obj.protocol = match[1]; | ||
obj.value = obj.value.substring(match[0].length); | ||
} | ||
function parse_host(obj) { | ||
if (obj.protocol == null) | ||
return; | ||
if (obj.protocol === 'file') { | ||
var match = rgx_win32Drive.exec(obj.value); | ||
if (match) { | ||
obj.host = match[1]; | ||
obj.value = obj.value.substring(obj.host.length); | ||
} | ||
return; | ||
} | ||
var pathStart = obj.value.indexOf('/', 2); | ||
obj.host = ~pathStart | ||
? obj.value.substring(0, pathStart) | ||
: obj.value; | ||
obj.value = obj.value.replace(obj.host, ''); | ||
} | ||
function parse_search(obj) { | ||
var question = obj.value.indexOf('?'); | ||
if (question === -1) | ||
return; | ||
obj.search = obj.value.substring(question); | ||
obj.value = obj.value.substring(0, question); | ||
} | ||
function parse_file(obj) { | ||
var match = rgx_fileWithExt.exec(obj.value), file = match == null ? null : match[1]; | ||
if (file == null) { | ||
return; | ||
} | ||
obj.file = file; | ||
obj.value = obj.value.substring(0, obj.value.length - file.length); | ||
obj.value = normalize_pathsSlashes(obj.value); | ||
match = rgx_extension.exec(file); | ||
obj.extension = match == null ? null : match[1]; | ||
} | ||
class_Uri.combinePathes = util_combinePathes; | ||
class_Uri.combine = util_combinePathes; | ||
}()); | ||
var class_EventEmitter; | ||
(function(){ | ||
class_EventEmitter = function () { | ||
this._listeners = {}; | ||
}; | ||
class_EventEmitter.prototype = { | ||
on: function (event, fn) { | ||
if (fn != null) { | ||
(this._listeners[event] || (this._listeners[event] = [])).push(fn); | ||
} | ||
return this; | ||
}, | ||
once: function (event, fn) { | ||
if (fn != null) { | ||
fn._once = true; | ||
(this._listeners[event] || (this._listeners[event] = [])).push(fn); | ||
} | ||
return this; | ||
}, | ||
pipe: function (event) { | ||
var that = this, args; | ||
return function () { | ||
args = _Array_slice.call(arguments); | ||
args.unshift(event); | ||
fn_apply(that.trigger, that, args); | ||
}; | ||
}, | ||
emit: event_trigger, | ||
trigger: event_trigger, | ||
off: function (event, fn) { | ||
var listeners = this._listeners[event]; | ||
if (listeners == null) | ||
return this; | ||
if (arguments.length === 1) { | ||
listeners.length = 0; | ||
return this; | ||
} | ||
var imax = listeners.length, i = -1; | ||
while (++i < imax) { | ||
if (listeners[i] === fn) { | ||
listeners.splice(i, 1); | ||
i--; | ||
imax--; | ||
} | ||
} | ||
return this; | ||
} | ||
}; | ||
function event_trigger() { | ||
var args = _Array_slice.call(arguments), event = args.shift(), fns = this._listeners[event], fn, imax, i = 0; | ||
if (fns == null) | ||
return this; | ||
for (imax = fns.length; i < imax; i++) { | ||
fn = fns[i]; | ||
fn_apply(fn, this, args); | ||
if (fn._once === true) { | ||
fns.splice(i, 1); | ||
i--; | ||
imax--; | ||
} | ||
} | ||
return this; | ||
} | ||
}()); | ||
Lib = { | ||
@@ -1071,0 +1071,0 @@ class_Dfr: class_Dfr, |
{ | ||
"name": "atma-utils", | ||
"description": "Helpers", | ||
"version": "0.1.23", | ||
"version": "0.1.24", | ||
"author": "Alexander Kit <alex.kit@atmajs.com>", | ||
@@ -6,0 +6,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package