1
0
mirror of synced 2025-12-10 16:38:16 +08:00

Dev: Use high resolution time for console time

This commit is contained in:
surunzi
2017-10-25 08:28:30 +08:00
parent 9af00cbf2d
commit 9917383830
5 changed files with 1343 additions and 117 deletions

View File

@@ -45,7 +45,7 @@ describe('log', function ()
{
tool.time('eruda');
tool.timeEnd('eruda');
expect($tool.find('.eruda-html')).toHaveText(/eruda: \d+ms/);
expect($tool.find('.eruda-html')).toHaveText(/eruda: [.\d]+ms/);
});
it('error', function ()

View File

@@ -5,27 +5,748 @@ window._ = (function()
if (typeof window === 'object' && window._) _ = window._;
/* ------------------------------ isUndef ------------------------------ */
var isUndef = _.isUndef = (function ()
{
/* Check if value is undefined.
*
* |Name |Type |Desc |
* |------|-------|--------------------------|
* |val |* |Value to check |
* |return|boolean|True if value is undefined|
*
* ```javascript
* isUndef(void 0); // -> true
* isUndef(null); // -> false
* ```
*/
/* module
* env: all
* test: all
*/
function exports(val)
{
return val === void 0;
}
return exports;
})();
/* ------------------------------ optimizeCb ------------------------------ */
var optimizeCb = _.optimizeCb = (function ()
{
/* Used for function context binding.
*/
/* module
* env: all
* test: all
*/
/* dependencies
* isUndef
*/
function exports(fn, ctx, argCount)
{
if (isUndef(ctx)) return fn;
switch (argCount == null ? 3 : argCount)
{
case 1: return function (val)
{
return fn.call(ctx, val);
};
case 3: return function (val, idx, collection)
{
return fn.call(ctx, val, idx, collection);
};
case 4: return function (accumulator, val, idx, collection)
{
return fn.call(ctx, accumulator, val, idx, collection);
}
}
return function ()
{
return fn.apply(ctx, arguments);
};
}
return exports;
})();
/* ------------------------------ toStr ------------------------------ */
var toStr = _.toStr = (function ()
{
/* Convert value to a string.
*
* |Name |Type |Desc |
* |------|------|----------------|
* |val |* |Value to convert|
* |return|string|Resulted string |
*
* ```javascript
* toStr(null); // -> ''
* toStr(1); // -> '1'
* toStr(false); // -> 'false'
* toStr([1, 2, 3]); // -> '1,2,3'
* ```
*/
/* module
* env: all
* test: all
*/
function exports(val)
{
return val == null ? '' : val.toString();
}
return exports;
})();
/* ------------------------------ has ------------------------------ */
var has = _.has = (function ()
{
/* Checks if key is a direct property.
*
* |Name |Type |Desc |
* |------|-------|--------------------------------|
* |obj |object |Object to query |
* |key |string |Path to check |
* |return|boolean|True if key is a direct property|
*
* ```javascript
* has({one: 1}, 'one'); // -> true
* ```
*/
/* module
* env: all
* test: all
*/
var hasOwnProp = Object.prototype.hasOwnProperty;
function exports(obj, key)
{
return hasOwnProp.call(obj, key);
}
return exports;
})();
/* ------------------------------ keys ------------------------------ */
var keys = _.keys = (function (exports)
{
/* Create an array of the own enumerable property names of object.
*
* |Name |Type |Desc |
* |------|------|-----------------------|
* |obj |object|Object to query |
* |return|array |Array of property names|
*
* ```javascript
* keys({a: 1}); // -> ['a']
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* has
*/
exports = Object.keys || function (obj)
{
var ret = [], key;
for (key in obj)
{
if (has(obj, key)) ret.push(key);
}
return ret;
};
return exports;
})({});
/* ------------------------------ identity ------------------------------ */
var identity = _.identity = (function ()
{
/* Return the first argument given.
*
* |Name |Type|Desc |
* |------|----|-----------|
* |val |* |Any value |
* |return|* |Given value|
*
* ```javascript
* identity('a'); // -> 'a'
* ```
*/
/* module
* env: all
* test: all
*/
function exports(val)
{
return val;
}
return exports;
})();
/* ------------------------------ objToStr ------------------------------ */
var objToStr = _.objToStr = (function ()
{
/* Alias of Object.prototype.toString.
*
* |Name |Type |Desc |
* |------|------|------------------------------------|
* |value |* |Source value |
* |return|string|String representation of given value|
*
* ```javascript
* objToStr(5); // -> '[object Number]'
* ```
*/
/* module
* env: all
* test: all
*/
var ObjToStr = Object.prototype.toString;
function exports(val)
{
return ObjToStr.call(val);
}
return exports;
})();
/* ------------------------------ isFn ------------------------------ */
var isFn = _.isFn = (function ()
{
/* Check if value is a function.
*
* |Name |Type |Desc |
* |------|-------|---------------------------|
* |val |* |Value to check |
* |return|boolean|True if value is a function|
*
* Generator function is also classified as true.
*
* ```javascript
* isFn(function() {}); // -> true
* isFn(function*() {}); // -> true
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* objToStr
*/
function exports(val)
{
var objStr = objToStr(val);
return objStr === '[object Function]' || objStr === '[object GeneratorFunction]';
}
return exports;
})();
/* ------------------------------ isNum ------------------------------ */
var isNum = _.isNum = (function ()
{
/* Checks if value is classified as a Number primitive or object.
*
* |Name |Type |Desc |
* |------|-------|-------------------------------------|
* |value |* |Value to check |
* |return|boolean|True if value is correctly classified|
*
* ```javascript
* isNum(5); // -> true
* isNum(5.1); // -> true
* isNum({}); // -> false
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* objToStr
*/
function exports(val)
{
return objToStr(val) === '[object Number]';
}
return exports;
})();
/* ------------------------------ isArrLike ------------------------------ */
var isArrLike = _.isArrLike = (function ()
{
/* Check if value is array-like.
*
* |Name |Type |Desc |
* |------|-------|---------------------------|
* |value |* |Value to check |
* |return|boolean|True if value is array like|
*
* > Function returns false.
*
* ```javascript
* isArrLike('test'); // -> true
* isArrLike(document.body.children); // -> true;
* isArrLike([1, 2, 3]); // -> true
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* isNum has isFn
*/
var MAX_ARR_IDX = Math.pow(2, 53) - 1;
function exports(val)
{
if (!has(val, 'length')) return false;
var len = val.length;
return isNum(len) && len >= 0 && len <= MAX_ARR_IDX && !isFn(val);
}
return exports;
})();
/* ------------------------------ each ------------------------------ */
var each = _.each = (function ()
{
/* Iterates over elements of collection and invokes iteratee for each element.
*
* |Name |Type |Desc |
* |--------|------------|------------------------------|
* |obj |object array|Collection to iterate over |
* |iteratee|function |Function invoked per iteration|
* |[ctx] |* |Function context |
*
* ```javascript
* each({'a': 1, 'b': 2}, function (val, key) {});
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* isArrLike keys optimizeCb
*/
function exports(obj, iteratee, ctx)
{
iteratee = optimizeCb(iteratee, ctx);
var i, len;
if (isArrLike(obj))
{
for (i = 0, len = obj.length; i < len; i++) iteratee(obj[i], i, obj);
} else
{
var _keys = keys(obj);
for (i = 0, len = _keys.length; i < len; i++)
{
iteratee(obj[_keys[i]], _keys[i], obj);
}
}
return obj;
}
return exports;
})();
/* ------------------------------ createAssigner ------------------------------ */
var createAssigner = _.createAssigner = (function ()
{
/* Used to create extend, extendOwn and defaults.
*
* |Name |Type |Desc |
* |--------|--------|------------------------------|
* |keysFn |function|Function to get object keys |
* |defaults|boolean |No override when set to true |
* |return |function|Result function, extend... |
*/
/* module
* env: all
* test: all
*/
/* dependencies
* isUndef each
*/
function exports(keysFn, defaults)
{
return function (obj)
{
each(arguments, function (src, idx)
{
if (idx === 0) return;
var keys = keysFn(src);
each(keys, function (key)
{
if (!defaults || isUndef(obj[key])) obj[key] = src[key];
});
});
return obj;
};
}
return exports;
})();
/* ------------------------------ extendOwn ------------------------------ */
var extendOwn = _.extendOwn = (function (exports)
{
/* Like extend, but only copies own properties over to the destination object.
*
* |Name |Type |Desc |
* |------|------|------------------|
* |obj |object|Destination object|
* |*src |object|Sources objects |
* |return|object|Destination object|
*
* ```javascript
* extendOwn({name: 'RedHood'}, {age: 24}); // -> {name: 'RedHood', age: 24}
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* keys createAssigner
*/
exports = createAssigner(keys);
return exports;
})({});
/* ------------------------------ isMatch ------------------------------ */
var isMatch = _.isMatch = (function ()
{
/* Check if keys and values in src are contained in obj.
*
* |Name |Type |Desc |
* |------|-------|----------------------------------|
* |obj |object |Object to inspect |
* |src |object |Object of property values to match|
* |return|boolean|True if object is match |
*
* ```javascript
* isMatch({a: 1, b: 2}, {a: 1}); // -> true
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* keys
*/
function exports(obj, src)
{
var _keys = keys(src),
len = _keys.length;
if (obj == null) return !len;
obj = Object(obj);
for (var i = 0; i < len; i++)
{
var key = _keys[i];
if (src[key] !== obj[key] || !(key in obj)) return false;
}
return true;
}
return exports;
})();
/* ------------------------------ isObj ------------------------------ */
var isObj = _.isObj = (function ()
{
/* Check if value is the language type of Object.
*
* |Name |Type |Desc |
* |------|-------|--------------------------|
* |val |* |Value to check |
* |return|boolean|True if value is an object|
*
* [Language Spec](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
*
* ```javascript
* isObj({}); // -> true
* isObj([]); // -> true
* ```
*/
/* module
* env: all
* test: all
*/
function exports(val)
{
var type = typeof val;
return !!val && (type === 'function' || type === 'object');
}
return exports;
})();
/* ------------------------------ matcher ------------------------------ */
var matcher = _.matcher = (function ()
{
/* Return a predicate function that checks if attrs are contained in an object.
*
* |Name |Type |Desc |
* |------|--------|----------------------------------|
* |attrs |object |Object of property values to match|
* |return|function|New predicate function |
*
* ```javascript
* var objects = [
* {a: 1, b: 2, c: 3 },
* {a: 4, b: 5, c: 6 }
* ];
* filter(objects, matcher({a: 4, c: 6 })); // -> [{a: 4, b: 5, c: 6 }]
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* extendOwn isMatch
*/
function exports(attrs)
{
attrs = extendOwn({}, attrs);
return function (obj)
{
return isMatch(obj, attrs);
};
}
return exports;
})();
/* ------------------------------ safeCb ------------------------------ */
var safeCb = _.safeCb = (function (exports)
{
/* Create callback based on input value.
*/
/* module
* env: all
* test: all
*/
/* dependencies
* isFn isObj optimizeCb matcher identity
*/
exports = function (val, ctx, argCount)
{
if (val == null) return identity;
if (isFn(val)) return optimizeCb(val, ctx, argCount);
if (isObj(val)) return matcher(val);
return function (key)
{
return function (obj)
{
return obj == null ? undefined : obj[key];
}
};
};
return exports;
})({});
/* ------------------------------ filter ------------------------------ */
var filter = _.filter = (function ()
{
/* Iterates over elements of collection, returning an array of all the values that pass a truth test.
*
* |Name |Type |Desc |
* |---------|--------|---------------------------------------|
* |obj |array |Collection to iterate over |
* |predicate|function|Function invoked per iteration |
* |[ctx] |* |Predicate context |
* |return |array |Array of all values that pass predicate|
*
* ```javascript
* filter([1, 2, 3, 4, 5], function (val)
* {
* return val % 2 === 0;
* }); // -> [2, 4]
* ```
*/
/* module
* env: all
* test: all
*/
/* dependencies
* safeCb each
*/
function exports(obj, predicate, ctx)
{
var ret = [];
predicate = safeCb(predicate, ctx);
each(obj, function (val, idx, list)
{
if (predicate(val, idx, list)) ret.push(val);
});
return ret;
}
return exports;
})();
/* ------------------------------ evalCss ------------------------------ */
_.evalCss = (function ()
{
var mark = [];
/* dependencies
* toStr each filter
*/
var styleList = [],
scale = 1;
function exports(css)
{
for (var i = 0, len = mark.length; i < len; i++)
css = toStr(css);
for (var i = 0, len = styleList.length; i < len; i++)
{
if (mark[i] === css) return;
if (styleList[i].css === css) return;
}
mark.push(css);
var container = exports.container || document.head,
style = document.createElement('style');
let container = exports.container || document.head,
el = document.createElement('style');
style.type = 'text/css';
style.textContent = css;
el.type = 'text/css';
container.appendChild(el);
container.appendChild(style);
let style = {css, el, container};
resetStyle(style);
styleList.push(style);
return style;
}
exports.setScale = function (s)
{
scale = s;
each(styleList, style => resetStyle(style));
};
exports.clear = function ()
{
each(styleList, ({container, el}) => container.removeChild(el));
styleList = [];
};
exports.remove = function (style)
{
styleList = filter(styleList, s => s !== style);
style.container.removeChild(style.el);
};
function resetStyle({css, el})
{
el.innerText = css.replace(/(\d+)px/g, ($0, $1) => (+$1 * scale) + 'px');
}
return exports;