142 lines
2.6 KiB
JavaScript
142 lines
2.6 KiB
JavaScript
|
|
var singleton = (function () {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
||
|
|
|
||
|
|
var client = typeof window != 'undefined';
|
||
|
|
|
||
|
|
var owner = client ? /* istanbul ignore next */ window : commonjsGlobal;
|
||
|
|
|
||
|
|
var is_1 = is;
|
||
|
|
is.fn = isFunction;
|
||
|
|
is.str = isString;
|
||
|
|
is.num = isNumber;
|
||
|
|
is.obj = isObject;
|
||
|
|
is.lit = isLiteral;
|
||
|
|
is.bol = isBoolean;
|
||
|
|
is.truthy = isTruthy;
|
||
|
|
is.falsy = isFalsy;
|
||
|
|
is.arr = isArray;
|
||
|
|
is.null = isNull;
|
||
|
|
is.def = isDef;
|
||
|
|
is.in = isIn;
|
||
|
|
is.promise = isPromise;
|
||
|
|
is.stream = isStream;
|
||
|
|
|
||
|
|
function is(v){
|
||
|
|
return function(d){
|
||
|
|
return d == v
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function isFunction(d) {
|
||
|
|
return typeof d == 'function'
|
||
|
|
}
|
||
|
|
|
||
|
|
function isBoolean(d) {
|
||
|
|
return typeof d == 'boolean'
|
||
|
|
}
|
||
|
|
|
||
|
|
function isString(d) {
|
||
|
|
return typeof d == 'string'
|
||
|
|
}
|
||
|
|
|
||
|
|
function isNumber(d) {
|
||
|
|
return typeof d == 'number'
|
||
|
|
}
|
||
|
|
|
||
|
|
function isObject(d) {
|
||
|
|
return typeof d == 'object'
|
||
|
|
}
|
||
|
|
|
||
|
|
function isLiteral(d) {
|
||
|
|
return typeof d == 'object'
|
||
|
|
&& !(d instanceof Array)
|
||
|
|
}
|
||
|
|
|
||
|
|
function isTruthy(d) {
|
||
|
|
return !!d == true
|
||
|
|
}
|
||
|
|
|
||
|
|
function isFalsy(d) {
|
||
|
|
return !!d == false
|
||
|
|
}
|
||
|
|
|
||
|
|
function isArray(d) {
|
||
|
|
return d instanceof Array
|
||
|
|
}
|
||
|
|
|
||
|
|
function isNull(d) {
|
||
|
|
return d === null
|
||
|
|
}
|
||
|
|
|
||
|
|
function isDef(d) {
|
||
|
|
return typeof d !== 'undefined'
|
||
|
|
}
|
||
|
|
|
||
|
|
function isPromise(d) {
|
||
|
|
return d instanceof Promise
|
||
|
|
}
|
||
|
|
|
||
|
|
function isStream(d) {
|
||
|
|
return !!(d && d.next)
|
||
|
|
}
|
||
|
|
|
||
|
|
function isIn(set) {
|
||
|
|
return function(d){
|
||
|
|
return !set ? false
|
||
|
|
: set.indexOf ? ~set.indexOf(d)
|
||
|
|
: d in set
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var to = {
|
||
|
|
arr: toArray
|
||
|
|
, obj: toObject
|
||
|
|
};
|
||
|
|
|
||
|
|
function toArray(d){
|
||
|
|
return Array.prototype.slice.call(d, 0)
|
||
|
|
}
|
||
|
|
|
||
|
|
function toObject(d) {
|
||
|
|
var by = 'id';
|
||
|
|
|
||
|
|
return arguments.length == 1
|
||
|
|
? (by = d, reduce)
|
||
|
|
: reduce.apply(this, arguments)
|
||
|
|
|
||
|
|
function reduce(p,v,i){
|
||
|
|
if (i === 0) { p = {}; }
|
||
|
|
p[is_1.fn(by) ? by(v, i) : v[by]] = v;
|
||
|
|
return p
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var log$1 = function log(ns){
|
||
|
|
return function(d){
|
||
|
|
if (!owner.console || !console.log.apply) { return d; }
|
||
|
|
is_1.arr(arguments[2]) && (arguments[2] = arguments[2].length);
|
||
|
|
var args = to.arr(arguments)
|
||
|
|
, prefix = '[log][' + (new Date()).toISOString() + ']' + ns;
|
||
|
|
|
||
|
|
args.unshift(prefix.grey ? prefix.grey : prefix);
|
||
|
|
return console.log.apply(console, args), d
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// -------------------------------------------
|
||
|
|
// Exposes a convenient global instance
|
||
|
|
// -------------------------------------------
|
||
|
|
var singleton = function singleton(ripple){
|
||
|
|
log('creating');
|
||
|
|
if (!owner.ripple) { owner.ripple = ripple; }
|
||
|
|
return ripple
|
||
|
|
};
|
||
|
|
|
||
|
|
var log = log$1('[ri/singleton]');
|
||
|
|
|
||
|
|
return singleton;
|
||
|
|
|
||
|
|
}());
|