Doc: Util api

This commit is contained in:
surunzi
2018-01-20 20:24:21 +08:00
parent 69a2938bb3
commit cec9e6a73d
13 changed files with 199 additions and 30 deletions

View File

@@ -788,6 +788,21 @@ castPath('a[0].b'); // -> ['a', '0', 'b']
castPath('a.b.c', {'a.b.c': true}); // -> ['a.b.c']
```
## chunk
Split array into groups the length of given size.
|Name |Type |Desc |
|--------|------|--------------------|
|arr |array |Array to process |
|[size=1]|number|Length of each chunk|
```javascript
chunk([1, 2, 3, 4], 2); // -> [[1, 2], [3, 4]]
chunk([1, 2, 3, 4], 3); // -> [[1, 2, 3], [4]]
chunk([1, 2, 3, 4]); // -> [[1], [2], [3], [4]]
```
## clamp
Clamp number within the inclusive lower and upper bounds.
@@ -1070,7 +1085,7 @@ escape('You & Me'); -> // -> 'You & Me'
## escapeJsonStr
No documentation.
Escape json string.
## escapeRegExp
@@ -1087,7 +1102,7 @@ escapeRegExp('[eris]'); // -> '\\[eris\\]'
## evalCss
No documentation.
Eval css.
## extend
@@ -1172,15 +1187,15 @@ console.log(a); // -> {b: 1}
## fullUrl
No documentation.
Add origin to url if needed.
## getFileName
No documentation.
Extract file name from url.
## getObjType
No documentation.
Get object type.
## has
@@ -1258,7 +1273,7 @@ Check if value is classified as an arguments object.
|Name |Type |Desc |
|------|-------|------------------------------------|
|value |* |Value to check |
|val |* |Value to check |
|return|boolean|True if value is an arguments object|
```javascript
@@ -1287,7 +1302,7 @@ Check if value is array-like.
|Name |Type |Desc |
|------|-------|---------------------------|
|value |* |Value to check |
|val |* |Value to check |
|return|boolean|True if value is array like|
> Function returns false.
@@ -1323,7 +1338,7 @@ console.log(isBrowser); // -> true if running in a browser
## isCrossOrig
No documentation.
Check if a url is cross origin.
## isDate
@@ -1381,7 +1396,7 @@ isErr(new Error()); // -> true
## isErudaEl
No documentation.
See if an element is within eruda.
## isFn
@@ -1448,7 +1463,7 @@ Check if value is a native function.
|Name |Type |Desc |
|------|-------|----------------------------------|
|value |* |Value to check |
|val |* |Value to check |
|return|boolean|True if value is a native function|
```javascript
@@ -1462,7 +1477,7 @@ Check if value is null or undefined, the same as value == null.
|Name |Type |Desc |
|------|-------|----------------------------------|
|value |* |Value to check |
|val |* |Value to check |
|return|boolean|True if value is null or undefined|
```javascript
@@ -1480,7 +1495,7 @@ Check if value is an Null.
|Name |Type |Desc |
|------|-------|------------------------|
|value |* |Value to check |
|val |* |Value to check |
|return|boolean|True if value is an Null|
```javascript
@@ -1493,7 +1508,7 @@ Check if value is classified as a Number primitive or object.
|Name |Type |Desc |
|------|-------|-------------------------------------|
|value |* |Value to check |
|val |* |Value to check |
|return|boolean|True if value is correctly classified|
```javascript
@@ -1876,7 +1891,7 @@ console.log(perfNow() - start);
## pxToNum
No documentation.
Turn string like '0px' to number.
## query
@@ -1980,7 +1995,7 @@ safeGet(obj, 'a.b'); // -> undefined
## safeStorage
No documentation.
Safe localStorage and sessionStorage.
## slice

View File

@@ -1,3 +1,6 @@
/* Escape json string.
*/
function exports(str)
{
return str.replace(/\\/g, '\\\\')

View File

@@ -1,3 +1,6 @@
/* Eval css.
*/
_('toStr each filter');
var styleList = [],

View File

@@ -1,3 +1,6 @@
/* Add origin to url if needed.
*/
_('startWith');
let origin = window.location.origin;

View File

@@ -1,3 +1,6 @@
/* Extract file name from url.
*/
_('last trim');
function exports(url)

View File

@@ -1,3 +1,6 @@
/* Get object type.
*/
_('upperFirst');
function exports(obj)

View File

@@ -1,3 +1,6 @@
/* Check if a url is cross origin.
*/
_('startWith');
var origin = window.location.origin;

View File

@@ -1,3 +1,6 @@
/* See if an element is within eruda.
*/
function exports(el)
{
var parentNode = el.parentNode;

View File

@@ -1,3 +1,6 @@
/* Turn string like '0px' to number.
*/
_('toNum');
function exports(str)

View File

@@ -1,3 +1,6 @@
/* Safe localStorage and sessionStorage.
*/
_('isUndef memStorage');
function exports(type, memReplacement)

View File

@@ -181,6 +181,9 @@ export var endWith = _.endWith = (function ()
export var escapeJsonStr = _.escapeJsonStr = (function ()
{
/* Escape json string.
*/
function exports(str)
{
return str.replace(/\\/g, '\\\\')
@@ -331,7 +334,7 @@ export var isArgs = _.isArgs = (function ()
*
* |Name |Type |Desc |
* |------|-------|------------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is an arguments object|
*
* ```javascript
@@ -400,7 +403,7 @@ export var isNum = _.isNum = (function ()
*
* |Name |Type |Desc |
* |------|-------|-------------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is correctly classified|
*
* ```javascript
@@ -473,7 +476,7 @@ export var isArrLike = _.isArrLike = (function ()
*
* |Name |Type |Desc |
* |------|-------|---------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is array like|
*
* > Function returns false.

View File

@@ -980,6 +980,9 @@ export var escape = _.escape = (function ()
export var escapeJsonStr = _.escapeJsonStr = (function ()
{
/* Escape json string.
*/
function exports(str)
{
return str.replace(/\\/g, '\\\\')
@@ -1063,6 +1066,9 @@ export var fileSize = _.fileSize = (function ()
export var fullUrl = _.fullUrl = (function ()
{
/* Add origin to url if needed.
*/
/* dependencies
* startWith
*/
@@ -1116,6 +1122,9 @@ export var upperFirst = _.upperFirst = (function ()
export var getObjType = _.getObjType = (function ()
{
/* Get object type.
*/
/* dependencies
* upperFirst
*/
@@ -1198,7 +1207,7 @@ export var isArgs = _.isArgs = (function ()
*
* |Name |Type |Desc |
* |------|-------|------------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is an arguments object|
*
* ```javascript
@@ -1437,7 +1446,7 @@ export var isNum = _.isNum = (function ()
*
* |Name |Type |Desc |
* |------|-------|-------------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is correctly classified|
*
* ```javascript
@@ -1472,7 +1481,7 @@ export var isArrLike = _.isArrLike = (function ()
*
* |Name |Type |Desc |
* |------|-------|---------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is array like|
*
* > Function returns false.
@@ -2037,6 +2046,9 @@ export var isBool = _.isBool = (function ()
export var isCrossOrig = _.isCrossOrig = (function ()
{
/* Check if a url is cross origin.
*/
/* dependencies
* startWith
*/
@@ -2117,6 +2129,9 @@ export var isErr = _.isErr = (function ()
export var isErudaEl = _.isErudaEl = (function ()
{
/* See if an element is within eruda.
*/
function exports(el)
{
var parentNode = el.parentNode;
@@ -2313,7 +2328,7 @@ export var isNil = _.isNil = (function ()
*
* |Name |Type |Desc |
* |------|-------|----------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is null or undefined|
*
* ```javascript
@@ -2397,7 +2412,7 @@ export var isNative = _.isNative = (function ()
*
* |Name |Type |Desc |
* |------|-------|----------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is a native function|
*
* ```javascript
@@ -2445,7 +2460,7 @@ export var isNull = _.isNull = (function ()
*
* |Name |Type |Desc |
* |------|-------|------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is an Null|
*
* ```javascript
@@ -2990,6 +3005,9 @@ export var filter = _.filter = (function ()
export var evalCss = _.evalCss = (function ()
{
/* Eval css.
*/
/* dependencies
* toStr each filter
*/
@@ -4745,6 +4763,9 @@ export var memStorage = _.memStorage = (function (exports)
export var safeStorage = _.safeStorage = (function ()
{
/* Safe localStorage and sessionStorage.
*/
/* dependencies
* isUndef memStorage
*/
@@ -5751,6 +5772,9 @@ export var toNum = _.toNum = (function (exports)
export var pxToNum = _.pxToNum = (function ()
{
/* Turn string like '0px' to number.
*/
/* dependencies
* toNum
*/
@@ -5997,6 +6021,9 @@ export var trim = _.trim = (function ()
export var getFileName = _.getFileName = (function ()
{
/* Extract file name from url.
*/
/* dependencies
* last trim
*/

View File

@@ -10,6 +10,8 @@
} else { root.util = factory(); }
}(this, function ()
{
"use strict";
var _ = {};
if (typeof window === 'object' && window.util) _ = window.util;
@@ -181,6 +183,38 @@
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 ()
@@ -321,7 +355,7 @@
*
* |Name |Type |Desc |
* |------|-------|------------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is an arguments object|
*
* ```javascript
@@ -390,11 +424,11 @@
var isNum = _.isNum = (function ()
{
/* Checks if value is classified as a Number primitive or object.
/* Check if value is classified as a Number primitive or object.
*
* |Name |Type |Desc |
* |------|-------|-------------------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is correctly classified|
*
* ```javascript
@@ -429,7 +463,7 @@
*
* |Name |Type |Desc |
* |------|-------|---------------------------|
* |value |* |Value to check |
* |val |* |Value to check |
* |return|boolean|True if value is array like|
*
* > Function returns false.
@@ -447,14 +481,14 @@
*/
/* dependencies
* isNum has isFn
* isNum isFn
*/
var MAX_ARR_IDX = Math.pow(2, 53) - 1;
function exports(val)
{
if (!has(val, 'length')) return false;
if (!val) return false;
var len = val.length;
@@ -1082,6 +1116,69 @@
return exports;
})();
/* ------------------------------ evalCss ------------------------------ */
_.evalCss = (function ()
{
/* Eval css.
*/
/* dependencies
* toStr each filter
*/
var styleList = [],
scale = 1;
function exports(css)
{
css = toStr(css);
for (var i = 0, len = styleList.length; i < len; i++)
{
if (styleList[i].css === css) return;
}
let container = exports.container || document.head,
el = document.createElement('style');
el.type = 'text/css';
container.appendChild(el);
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;
})();
/* ------------------------------ map ------------------------------ */
var map = _.map = (function ()