atma-utils
Advanced tools
Comparing version 0.1.28 to 0.1.30
@@ -147,3 +147,4 @@ var _Array_slice, | ||
}()); | ||
var obj_getProperty, | ||
var obj_copyProperty, | ||
obj_getProperty, | ||
obj_setProperty, | ||
@@ -159,4 +160,20 @@ obj_hasProperty, | ||
_Object_create, | ||
obj_create; | ||
obj_create, | ||
obj_defaults, | ||
obj_extendDescriptors, | ||
obj_extendDescriptorsDefaults; | ||
(function(){ | ||
var getDescriptor = Object.getOwnPropertyDescriptor; | ||
var defineDescriptor = Object.defineProperty; | ||
obj_copyProperty = getDescriptor == null | ||
? function (target, source, key) { return target[key] = source[key]; } | ||
: function (target, source, key) { | ||
var descr = getDescriptor(source, key); | ||
if (descr.value !== void 0) { | ||
target[key] = descr.value; | ||
return; | ||
} | ||
defineDescriptor(target, key, descr); | ||
}; | ||
obj_getProperty = function (obj_, path) { | ||
@@ -306,3 +323,48 @@ if (path.indexOf('.') === -1) { | ||
obj_create = _Object_create; | ||
obj_defaults = function (target, defaults) { | ||
for (var key in defaults) { | ||
if (target[key] == null) | ||
target[key] = defaults[key]; | ||
} | ||
return target; | ||
} | ||
obj_extendDescriptors; | ||
obj_extendDescriptorsDefaults; | ||
(function () { | ||
if (getDescriptor == null) { | ||
obj_extendDescriptors = obj_extend; | ||
obj_extendDescriptorsDefaults = obj_defaults; | ||
return; | ||
} | ||
obj_extendDescriptors = function (target, source) { | ||
return _extendDescriptors(target, source, false); | ||
}; | ||
obj_extendDescriptorsDefaults = function (target, source) { | ||
return _extendDescriptors(target, source, true); | ||
}; | ||
function _extendDescriptors(target, source, defaultsOnly) { | ||
if (target == null) | ||
return {}; | ||
if (source == null) | ||
return source; | ||
var descr, key; | ||
for (key in source) { | ||
if (defaultsOnly === true && target[key] != null) | ||
continue; | ||
descr = getDescriptor(source, key); | ||
if (descr == null) { | ||
obj_extendDescriptors(target, source["__proto__"]); | ||
continue; | ||
} | ||
if (descr.value !== void 0) { | ||
target[key] = descr.value; | ||
continue; | ||
} | ||
defineDescriptor(target, key, descr); | ||
} | ||
return target; | ||
} | ||
})(); | ||
}()); | ||
@@ -1181,2 +1243,223 @@ var arr_remove, | ||
}()); | ||
var mixin; | ||
(function(){ | ||
var class_inherit, | ||
class_extendProtoObjects; | ||
(function(){ | ||
var PROTO = "__proto__"; | ||
var _toString = Object.prototype.toString; | ||
var _isArguments = function (args) { | ||
return _toString.call(args) === "[object Arguments]"; | ||
}; | ||
class_inherit = PROTO in Object.prototype ? inherit : inherit_protoLess; | ||
class_extendProtoObjects = function (proto, _base, _extends) { | ||
var key, protoValue; | ||
for (key in proto) { | ||
protoValue = proto[key]; | ||
if (!is_rawObject(protoValue)) | ||
continue; | ||
if (_base != null) { | ||
if (is_rawObject(_base.prototype[key])) | ||
obj_defaults(protoValue, _base.prototype[key]); | ||
} | ||
if (_extends != null) { | ||
arr_each(_extends, proto_extendDefaultsDelegate(protoValue, key)); | ||
} | ||
} | ||
} | ||
; | ||
// PRIVATE | ||
function proto_extendDefaultsDelegate(target, key) { | ||
return function (source) { | ||
var proto = proto_getProto(source), val = proto[key]; | ||
if (is_rawObject(val)) { | ||
obj_defaults(target, val); | ||
} | ||
}; | ||
} | ||
function proto_extend(proto, source) { | ||
if (source == null) | ||
return; | ||
if (typeof proto === "function") | ||
proto = proto.prototype; | ||
if (typeof source === "function") | ||
source = source.prototype; | ||
var key, val; | ||
for (key in source) { | ||
if (key === "constructor") | ||
continue; | ||
val = source[key]; | ||
if (val != null) | ||
proto[key] = val; | ||
} | ||
} | ||
function proto_override(super_, fn) { | ||
var proxy; | ||
if (super_) { | ||
proxy = function (mix) { | ||
var args = arguments.length === 1 && _isArguments(mix) ? mix : arguments; | ||
return fn_apply(super_, this, args); | ||
}; | ||
} | ||
else { | ||
proxy = fn_doNothing; | ||
} | ||
return function () { | ||
this["super"] = proxy; | ||
return fn_apply(fn, this, arguments); | ||
}; | ||
} | ||
function inherit(_class, _base, _extends, original) { | ||
var prototype = original, proto = original; | ||
prototype.constructor = _class.prototype.constructor; | ||
if (_extends != null) { | ||
proto[PROTO] = {}; | ||
arr_each(_extends, function (x) { | ||
proto_extend(proto[PROTO], x); | ||
}); | ||
proto = proto[PROTO]; | ||
} | ||
if (_base != null) | ||
proto[PROTO] = _base.prototype; | ||
_class.prototype = prototype; | ||
} | ||
function inherit_Object_create(_class, _base, _extends, original, _overrides, defaults) { | ||
if (_base != null) { | ||
_class.prototype = Object.create(_base.prototype); | ||
obj_extendDescriptors(_class.prototype, original); | ||
} | ||
else { | ||
_class.prototype = Object.create(original); | ||
} | ||
_class.prototype.constructor = _class; | ||
if (_extends != null) { | ||
arr_each(_extends, function (x) { | ||
obj_defaults(_class.prototype, x); | ||
}); | ||
} | ||
var proto = _class.prototype; | ||
obj_defaults(proto, defaults); | ||
for (var key in _overrides) { | ||
proto[key] = proto_override(proto[key], _overrides[key]); | ||
} | ||
} | ||
// browser that doesnt support __proto__ | ||
function inherit_protoLess(_class, _base, _extends, original) { | ||
if (_base != null) { | ||
var tmp = function () { }; | ||
tmp.prototype = _base.prototype; | ||
_class.prototype = new tmp(); | ||
_class.prototype.constructor = _class; | ||
} | ||
if (_extends != null) { | ||
arr_each(_extends, function (x) { | ||
delete x.constructor; | ||
proto_extend(_class, x); | ||
}); | ||
} | ||
proto_extend(_class, original); | ||
} | ||
function proto_getProto(mix) { | ||
return is_Function(mix) ? mix.prototype : mix; | ||
} | ||
}()); | ||
mixin = function (mix1, mix2, mix3, mix4, mix5) { | ||
return mix(mix1, mix2, mix3, mix4, mix5); | ||
} | ||
function mix() { | ||
var mixins = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
mixins[_i] = arguments[_i]; | ||
} | ||
var _base = mixins[0]; | ||
var _extends = mixins.slice(1); | ||
var _callable = ensureCallable(mixins); | ||
var _class = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
for (var i = _callable.length - 1; i > -1; i--) { | ||
var x = _callable[i]; | ||
if (typeof x === 'function') { | ||
fn_apply(x, this, args); | ||
} | ||
} | ||
}; | ||
if (is_Function(_base) === false) { | ||
_extends.unshift(_base); | ||
_base = null; | ||
} | ||
mixStatics(_class, mixins); | ||
var proto = {}; | ||
class_extendProtoObjects(proto, _base, _extends); | ||
class_inherit(_class, _base, _extends, proto); | ||
return _class; | ||
} | ||
function mixStatics(Ctor, mixins) { | ||
for (var i = 0; i < mixins.length; i++) { | ||
var Fn = mixins[i]; | ||
if (typeof Fn !== 'function') { | ||
continue; | ||
} | ||
for (var key in Fn) { | ||
if (key in Ctor === false) { | ||
obj_copyProperty(Ctor, Fn, key); | ||
} | ||
} | ||
} | ||
} | ||
var ensureCallableSingle, ensureCallable; | ||
(function () { | ||
ensureCallable = function (arr) { | ||
var out = [], i = arr.length; | ||
while (--i > -1) | ||
out[i] = ensureCallableSingle(arr[i]); | ||
return out; | ||
}; | ||
ensureCallableSingle = function (mix) { | ||
if (is_Function(mix) === false) { | ||
return mix; | ||
} | ||
var fn = mix; | ||
var caller = directCaller; | ||
var safe = false; | ||
var wrapped = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var self = this; | ||
var x; | ||
if (safe === true) { | ||
caller(fn, self, args); | ||
return; | ||
} | ||
try { | ||
x = caller(fn, self, args); | ||
safe = true; | ||
} | ||
catch (error) { | ||
caller = newCaller; | ||
safe = true; | ||
caller(fn, self, args); | ||
} | ||
if (x != null) { | ||
return x; | ||
} | ||
}; | ||
return wrapped; | ||
}; | ||
function directCaller(fn, self, args) { | ||
return fn.apply(self, args); | ||
} | ||
function newCaller(fn, self, args) { | ||
var x = new (fn.bind.apply(fn, [null].concat(args))); | ||
obj_extend(self, x); | ||
} | ||
}()); | ||
}()); |
289
lib/utils.js
@@ -29,3 +29,4 @@ (function(factory){ | ||
_Object_defineProperty; | ||
var obj_getProperty, | ||
var obj_copyProperty, | ||
obj_getProperty, | ||
obj_setProperty, | ||
@@ -39,3 +40,5 @@ obj_hasProperty, | ||
obj_extendMany, | ||
obj_create; | ||
obj_create, | ||
obj_defaults, | ||
obj_extendDescriptors; | ||
(function(){ | ||
@@ -57,2 +60,15 @@ (function(){ | ||
}()); | ||
var getDescriptor = Object.getOwnPropertyDescriptor; | ||
var defineDescriptor = Object.defineProperty; | ||
obj_copyProperty = getDescriptor == null | ||
? function (target, source, key) { return target[key] = source[key]; } | ||
: function (target, source, key) { | ||
var descr = getDescriptor(source, key); | ||
if (descr.value !== void 0) { | ||
target[key] = descr.value; | ||
return; | ||
} | ||
defineDescriptor(target, key, descr); | ||
}; | ||
obj_getProperty = function (obj_, path) { | ||
@@ -202,3 +218,48 @@ if (path.indexOf('.') === -1) { | ||
obj_create = _Object_create; | ||
obj_defaults = function (target, defaults) { | ||
for (var key in defaults) { | ||
if (target[key] == null) | ||
target[key] = defaults[key]; | ||
} | ||
return target; | ||
} | ||
obj_extendDescriptors; | ||
var obj_extendDescriptorsDefaults; | ||
(function () { | ||
if (getDescriptor == null) { | ||
obj_extendDescriptors = obj_extend; | ||
obj_extendDescriptorsDefaults = obj_defaults; | ||
return; | ||
} | ||
obj_extendDescriptors = function (target, source) { | ||
return _extendDescriptors(target, source, false); | ||
}; | ||
obj_extendDescriptorsDefaults = function (target, source) { | ||
return _extendDescriptors(target, source, true); | ||
}; | ||
function _extendDescriptors(target, source, defaultsOnly) { | ||
if (target == null) | ||
return {}; | ||
if (source == null) | ||
return source; | ||
var descr, key; | ||
for (key in source) { | ||
if (defaultsOnly === true && target[key] != null) | ||
continue; | ||
descr = getDescriptor(source, key); | ||
if (descr == null) { | ||
obj_extendDescriptors(target, source["__proto__"]); | ||
continue; | ||
} | ||
if (descr.value !== void 0) { | ||
target[key] = descr.value; | ||
continue; | ||
} | ||
defineDescriptor(target, key, descr); | ||
} | ||
return target; | ||
} | ||
})(); | ||
}()); | ||
@@ -1090,2 +1151,223 @@ var class_create, | ||
}()); | ||
var mixin; | ||
(function(){ | ||
var class_inherit, | ||
class_extendProtoObjects; | ||
(function(){ | ||
var PROTO = "__proto__"; | ||
var _toString = Object.prototype.toString; | ||
var _isArguments = function (args) { | ||
return _toString.call(args) === "[object Arguments]"; | ||
}; | ||
class_inherit = PROTO in Object.prototype ? inherit : inherit_protoLess; | ||
class_extendProtoObjects = function (proto, _base, _extends) { | ||
var key, protoValue; | ||
for (key in proto) { | ||
protoValue = proto[key]; | ||
if (!is_rawObject(protoValue)) | ||
continue; | ||
if (_base != null) { | ||
if (is_rawObject(_base.prototype[key])) | ||
obj_defaults(protoValue, _base.prototype[key]); | ||
} | ||
if (_extends != null) { | ||
arr_each(_extends, proto_extendDefaultsDelegate(protoValue, key)); | ||
} | ||
} | ||
} | ||
; | ||
// PRIVATE | ||
function proto_extendDefaultsDelegate(target, key) { | ||
return function (source) { | ||
var proto = proto_getProto(source), val = proto[key]; | ||
if (is_rawObject(val)) { | ||
obj_defaults(target, val); | ||
} | ||
}; | ||
} | ||
function proto_extend(proto, source) { | ||
if (source == null) | ||
return; | ||
if (typeof proto === "function") | ||
proto = proto.prototype; | ||
if (typeof source === "function") | ||
source = source.prototype; | ||
var key, val; | ||
for (key in source) { | ||
if (key === "constructor") | ||
continue; | ||
val = source[key]; | ||
if (val != null) | ||
proto[key] = val; | ||
} | ||
} | ||
function proto_override(super_, fn) { | ||
var proxy; | ||
if (super_) { | ||
proxy = function (mix) { | ||
var args = arguments.length === 1 && _isArguments(mix) ? mix : arguments; | ||
return fn_apply(super_, this, args); | ||
}; | ||
} | ||
else { | ||
proxy = fn_doNothing; | ||
} | ||
return function () { | ||
this["super"] = proxy; | ||
return fn_apply(fn, this, arguments); | ||
}; | ||
} | ||
function inherit(_class, _base, _extends, original) { | ||
var prototype = original, proto = original; | ||
prototype.constructor = _class.prototype.constructor; | ||
if (_extends != null) { | ||
proto[PROTO] = {}; | ||
arr_each(_extends, function (x) { | ||
proto_extend(proto[PROTO], x); | ||
}); | ||
proto = proto[PROTO]; | ||
} | ||
if (_base != null) | ||
proto[PROTO] = _base.prototype; | ||
_class.prototype = prototype; | ||
} | ||
function inherit_Object_create(_class, _base, _extends, original, _overrides, defaults) { | ||
if (_base != null) { | ||
_class.prototype = Object.create(_base.prototype); | ||
obj_extendDescriptors(_class.prototype, original); | ||
} | ||
else { | ||
_class.prototype = Object.create(original); | ||
} | ||
_class.prototype.constructor = _class; | ||
if (_extends != null) { | ||
arr_each(_extends, function (x) { | ||
obj_defaults(_class.prototype, x); | ||
}); | ||
} | ||
var proto = _class.prototype; | ||
obj_defaults(proto, defaults); | ||
for (var key in _overrides) { | ||
proto[key] = proto_override(proto[key], _overrides[key]); | ||
} | ||
} | ||
// browser that doesnt support __proto__ | ||
function inherit_protoLess(_class, _base, _extends, original) { | ||
if (_base != null) { | ||
var tmp = function () { }; | ||
tmp.prototype = _base.prototype; | ||
_class.prototype = new tmp(); | ||
_class.prototype.constructor = _class; | ||
} | ||
if (_extends != null) { | ||
arr_each(_extends, function (x) { | ||
delete x.constructor; | ||
proto_extend(_class, x); | ||
}); | ||
} | ||
proto_extend(_class, original); | ||
} | ||
function proto_getProto(mix) { | ||
return is_Function(mix) ? mix.prototype : mix; | ||
} | ||
}()); | ||
mixin = function (mix1, mix2, mix3, mix4, mix5) { | ||
return mix(mix1, mix2, mix3, mix4, mix5); | ||
} | ||
function mix() { | ||
var mixins = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
mixins[_i] = arguments[_i]; | ||
} | ||
var _base = mixins[0]; | ||
var _extends = mixins.slice(1); | ||
var _callable = ensureCallable(mixins); | ||
var _class = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
for (var i = _callable.length - 1; i > -1; i--) { | ||
var x = _callable[i]; | ||
if (typeof x === 'function') { | ||
fn_apply(x, this, args); | ||
} | ||
} | ||
}; | ||
if (is_Function(_base) === false) { | ||
_extends.unshift(_base); | ||
_base = null; | ||
} | ||
mixStatics(_class, mixins); | ||
var proto = {}; | ||
class_extendProtoObjects(proto, _base, _extends); | ||
class_inherit(_class, _base, _extends, proto); | ||
return _class; | ||
} | ||
function mixStatics(Ctor, mixins) { | ||
for (var i = 0; i < mixins.length; i++) { | ||
var Fn = mixins[i]; | ||
if (typeof Fn !== 'function') { | ||
continue; | ||
} | ||
for (var key in Fn) { | ||
if (key in Ctor === false) { | ||
obj_copyProperty(Ctor, Fn, key); | ||
} | ||
} | ||
} | ||
} | ||
var ensureCallableSingle, ensureCallable; | ||
(function () { | ||
ensureCallable = function (arr) { | ||
var out = [], i = arr.length; | ||
while (--i > -1) | ||
out[i] = ensureCallableSingle(arr[i]); | ||
return out; | ||
}; | ||
ensureCallableSingle = function (mix) { | ||
if (is_Function(mix) === false) { | ||
return mix; | ||
} | ||
var fn = mix; | ||
var caller = directCaller; | ||
var safe = false; | ||
var wrapped = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var self = this; | ||
var x; | ||
if (safe === true) { | ||
caller(fn, self, args); | ||
return; | ||
} | ||
try { | ||
x = caller(fn, self, args); | ||
safe = true; | ||
} | ||
catch (error) { | ||
caller = newCaller; | ||
safe = true; | ||
caller(fn, self, args); | ||
} | ||
if (x != null) { | ||
return x; | ||
} | ||
}; | ||
return wrapped; | ||
}; | ||
function directCaller(fn, self, args) { | ||
return fn.apply(self, args); | ||
} | ||
function newCaller(fn, self, args) { | ||
var x = new (fn.bind.apply(fn, [null].concat(args))); | ||
obj_extend(self, x); | ||
} | ||
}()); | ||
}()); | ||
var Lib = { | ||
@@ -1126,3 +1408,4 @@ class_Dfr: class_Dfr, | ||
str_format: str_format, | ||
str_dedent: str_dedent | ||
str_dedent: str_dedent, | ||
mixin: mixin | ||
}; | ||
@@ -1129,0 +1412,0 @@ |
{ | ||
"name": "atma-utils", | ||
"description": "Helpers", | ||
"version": "0.1.28", | ||
"version": "0.1.30", | ||
"author": "Alexander Kit <alex.kit@atmajs.com>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -87,2 +87,34 @@ declare module "atma-utils" { | ||
} | ||
export type Constructor<T = {}> = { | ||
new (...args: any[]): T; | ||
}; | ||
export type Statics<T> = { [P in keyof T]: T[P] }; | ||
export function mixin< | ||
T1 extends Constructor | object, | ||
T2 extends Constructor | object = {}, | ||
T3 extends Constructor | object = {}, | ||
T4 extends Constructor | object = {}, | ||
T5 extends Constructor | object = {} | ||
>( | ||
mix1: T1, | ||
mix2?: T2, | ||
mix3?: T3, | ||
mix4?: T4, | ||
mix5?: T5 | ||
): (T1 extends Constructor ? Statics<T1> : {}) & | ||
(T2 extends Constructor ? Statics<T2> : {}) & | ||
(T3 extends Constructor ? Statics<T3> : {}) & | ||
(T4 extends Constructor ? Statics<T4> : {}) & | ||
(T5 extends Constructor ? Statics<T5> : {}) & | ||
(new ( | ||
...args: T1 extends Constructor ? ConstructorParameters<T1> : never[] | ||
) => (T1 extends Constructor ? InstanceType<T1> : T1) & | ||
(T2 extends Constructor ? InstanceType<T2> : T2) & | ||
(T3 extends Constructor ? InstanceType<T3> : T3) & | ||
(T4 extends Constructor ? InstanceType<T4> : T4) & | ||
(T5 extends Constructor ? InstanceType<T5> : T5)) | ||
; | ||
} |
@@ -23,3 +23,2 @@ export function is_Function(mix: any): boolean | ||
export class class_EventEmitter { | ||
@@ -87,2 +86,34 @@ on: (event: string, cb: Function) => this | ||
static combine (...paths: string[]): string | ||
} | ||
} | ||
export type Constructor<T = {}> = { | ||
new (...args: any[]): T; | ||
}; | ||
export type Statics<T> = { [P in keyof T]: T[P] }; | ||
export function mixin< | ||
T1 extends Constructor | object, | ||
T2 extends Constructor | object = {}, | ||
T3 extends Constructor | object = {}, | ||
T4 extends Constructor | object = {}, | ||
T5 extends Constructor | object = {} | ||
>( | ||
mix1: T1, | ||
mix2?: T2, | ||
mix3?: T3, | ||
mix4?: T4, | ||
mix5?: T5 | ||
): (T1 extends Constructor ? Statics<T1> : {}) & | ||
(T2 extends Constructor ? Statics<T2> : {}) & | ||
(T3 extends Constructor ? Statics<T3> : {}) & | ||
(T4 extends Constructor ? Statics<T4> : {}) & | ||
(T5 extends Constructor ? Statics<T5> : {}) & | ||
(new ( | ||
...args: T1 extends Constructor ? ConstructorParameters<T1> : never[] | ||
) => (T1 extends Constructor ? InstanceType<T1> : T1) & | ||
(T2 extends Constructor ? InstanceType<T2> : T2) & | ||
(T3 extends Constructor ? InstanceType<T3> : T3) & | ||
(T4 extends Constructor ? InstanceType<T4> : T4) & | ||
(T5 extends Constructor ? InstanceType<T5> : T5)) | ||
; |
@@ -1,3 +0,4 @@ | ||
import { is_Function } from './module' | ||
import { is_Function, mixin } from './module' | ||
is_Function () | ||
is_Function ({}) | ||
mixin({a: 1}, { b: 1}) |
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
176743
5657