From cec9e6a73db5863128bf00156c1919ec63b78a76 Mon Sep 17 00:00:00 2001 From: surunzi Date: Sat, 20 Jan 2018 20:24:21 +0800 Subject: [PATCH] Doc: Util api --- doc/UTIL_API.md | 45 ++++++++++------ eustia/escapeJsonStr.js | 3 ++ eustia/evalCss.js | 3 ++ eustia/fullUrl.js | 3 ++ eustia/getFileName.js | 3 ++ eustia/getObjType.js | 3 ++ eustia/isCrossOrig.js | 3 ++ eustia/isErudaEl.js | 3 ++ eustia/pxToNum.js | 3 ++ eustia/safeStorage.js | 3 ++ src/lib/stringifyUtil.js | 9 ++-- src/lib/util.js | 39 +++++++++++--- test/util.js | 109 ++++++++++++++++++++++++++++++++++++--- 13 files changed, 199 insertions(+), 30 deletions(-) diff --git a/doc/UTIL_API.md b/doc/UTIL_API.md index 970203f..9eb6134 100644 --- a/doc/UTIL_API.md +++ b/doc/UTIL_API.md @@ -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 diff --git a/eustia/escapeJsonStr.js b/eustia/escapeJsonStr.js index 6b2f56d..59bb49f 100644 --- a/eustia/escapeJsonStr.js +++ b/eustia/escapeJsonStr.js @@ -1,3 +1,6 @@ +/* Escape json string. + */ + function exports(str) { return str.replace(/\\/g, '\\\\') diff --git a/eustia/evalCss.js b/eustia/evalCss.js index 336ef8b..4d05dc0 100644 --- a/eustia/evalCss.js +++ b/eustia/evalCss.js @@ -1,3 +1,6 @@ +/* Eval css. + */ + _('toStr each filter'); var styleList = [], diff --git a/eustia/fullUrl.js b/eustia/fullUrl.js index 16797d9..1bd8e53 100644 --- a/eustia/fullUrl.js +++ b/eustia/fullUrl.js @@ -1,3 +1,6 @@ +/* Add origin to url if needed. + */ + _('startWith'); let origin = window.location.origin; diff --git a/eustia/getFileName.js b/eustia/getFileName.js index 9311f57..44935d4 100644 --- a/eustia/getFileName.js +++ b/eustia/getFileName.js @@ -1,3 +1,6 @@ +/* Extract file name from url. + */ + _('last trim'); function exports(url) diff --git a/eustia/getObjType.js b/eustia/getObjType.js index 0edb834..733711f 100644 --- a/eustia/getObjType.js +++ b/eustia/getObjType.js @@ -1,3 +1,6 @@ +/* Get object type. + */ + _('upperFirst'); function exports(obj) diff --git a/eustia/isCrossOrig.js b/eustia/isCrossOrig.js index e4cac95..37358ab 100644 --- a/eustia/isCrossOrig.js +++ b/eustia/isCrossOrig.js @@ -1,3 +1,6 @@ +/* Check if a url is cross origin. + */ + _('startWith'); var origin = window.location.origin; diff --git a/eustia/isErudaEl.js b/eustia/isErudaEl.js index 270e6b4..8f59dc2 100644 --- a/eustia/isErudaEl.js +++ b/eustia/isErudaEl.js @@ -1,3 +1,6 @@ +/* See if an element is within eruda. + */ + function exports(el) { var parentNode = el.parentNode; diff --git a/eustia/pxToNum.js b/eustia/pxToNum.js index 158cc12..bc639d3 100644 --- a/eustia/pxToNum.js +++ b/eustia/pxToNum.js @@ -1,3 +1,6 @@ +/* Turn string like '0px' to number. + */ + _('toNum'); function exports(str) diff --git a/eustia/safeStorage.js b/eustia/safeStorage.js index ac7b31f..0e68534 100644 --- a/eustia/safeStorage.js +++ b/eustia/safeStorage.js @@ -1,3 +1,6 @@ +/* Safe localStorage and sessionStorage. + */ + _('isUndef memStorage'); function exports(type, memReplacement) diff --git a/src/lib/stringifyUtil.js b/src/lib/stringifyUtil.js index 914ac86..cb4ecd0 100644 --- a/src/lib/stringifyUtil.js +++ b/src/lib/stringifyUtil.js @@ -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. diff --git a/src/lib/util.js b/src/lib/util.js index eda1b00..a7ff5a4 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -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 */ diff --git a/test/util.js b/test/util.js index f733edb..89b2c32 100644 --- a/test/util.js +++ b/test/util.js @@ -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 ()