diff --git a/README.md b/README.md index 1792be9..f51b870 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ console.log(validNumber('1.2.')); // 1.20 `lib` 目录下,每一个目录是一个函数类型集合。 -每个目录下,不能再创建目录,只能创建函数文件。 +每个目录下,不能再创建目录,只能创建函数文件。 函数名称尽量简短且清楚地表达函数的作用。 @@ -129,14 +129,3 @@ console.log(validNumber('1.2.')); // 1.20 1. 每个注册用户可以免费领取一个永久注册码(登录自动领取)。 2. 每个贡献者可以额外领取一个(私聊我即可)。 - -### 贡献者 - - - - - - - - - diff --git a/lib/array/countOccurrences.js b/lib/array/countOccurrences.js index 1473cef..9716fff 100644 --- a/lib/array/countOccurrences.js +++ b/lib/array/countOccurrences.js @@ -2,8 +2,9 @@ * 统计数组中各项出现的次数 * @author 橙某人 * @category 数组 + * @alias yd_array_countOccurrences * @param {Array} array 任意一维数组 - * @returns {Object} 返回以每项为key,次数为value的对象 + * @returns {object} 返回以每项为key,次数为value的对象 * * @example * console.log(yd_array_countOccurrences([1, 1, 2, 3, 3, 4, 4, 4])); // {1: 2, 2: 1, 3: 2, 4: 3} @@ -11,4 +12,16 @@ * @example * console.log(yd_array_countOccurrences(['a', 'a', 'bb', 'abc', 'fff', 'fff', '橙', '橙'])); // {a: 2, bb: 1, abc: 1, fff: 2, 橙: 2} */ -export default (arr) => arr.reduce((prev, curr) => ((prev[curr] = ++prev[curr] || 1), prev), {}); +export default (array) => { + const occurrences = {}; + for (let i = 0; i < array.length; i++) { + const element = array[i]; + if (occurrences[element]) { + occurrences[element]++; + } + else { + occurrences[element] = 1; + } + } + return occurrences; +}; diff --git a/lib/array/diffBoth.js b/lib/array/diffBoth.js index 884392b..42e6207 100644 --- a/lib/array/diffBoth.js +++ b/lib/array/diffBoth.js @@ -2,6 +2,7 @@ * 数组的差集 * @author 陈随易 * @category 数组 + * @alias yd_array_diffBoth * @param {Array} arr1 数组1 * @param {Array} arr2 数组2 * @returns Array 数组的差集 diff --git a/lib/array/diffFirst.js b/lib/array/diffFirst.js index bd84c43..5cfa7d1 100644 --- a/lib/array/diffFirst.js +++ b/lib/array/diffFirst.js @@ -2,6 +2,7 @@ * 取第一个数组的差集 * @author 陈随易 * @category 数组 + * @alias yd_array_diffFirst * @param {Array} arr1 数组1 * @param {Array} arr2 数组2 * @returns {Array} 返回第一个数组中跟第二个数组不同的值 @@ -9,7 +10,7 @@ export default (arr1, arr2) => { const set1 = new Set(arr1); const set2 = new Set(arr2); - const uniqueElements = new Set([...set1].filter((x) => !set2.has(x))); + const uniqueElements = new Set([...set1].filter(x => !set2.has(x))); return Array.from(uniqueElements); -}; +} diff --git a/lib/array/findIndex.js b/lib/array/findIndex.js index 875bd7e..b09d4a7 100644 --- a/lib/array/findIndex.js +++ b/lib/array/findIndex.js @@ -2,16 +2,17 @@ * 查找数组中匹配的索引 * @author 陈随易 * @category 数组 + * @alias yd_array_findIndex * @param {Array} array 数组 - * @param {String} field 要查找的字段 - * @param {String} value 该字段的值 - * @returns {Number} 返回匹配的索引 + * @param {string} field 要查找的字段 + * @param {string} value 该字段的值 + * @returns {number} 返回匹配的索引 */ export default (array, field, value) => { let result = null; for (const [index, item] of array.entries()) { - for (let prop in item) { - if (item.hasOwnProperty(prop)) { + for (const prop in item) { + if (Object.prototype.hasOwnProperty.call(item, prop)) { if (prop === field && item[prop] === value) { result = index; break; diff --git a/lib/array/findObj.js b/lib/array/findObj.js index 1905bb3..b6925b6 100644 --- a/lib/array/findObj.js +++ b/lib/array/findObj.js @@ -2,16 +2,17 @@ * 查找数组中匹配的值 * @author 陈随易 * @category 数组 - * @param {Array} array 数组对象 - * @param {String} field 要查找的属性 + * @alias yd_array_findObj + * @param {Array} arrObj 数组对象 + * @param {string} field 要查找的属性 * @param {Any} value 属性的值 - * @returns {Object} 返回匹配的对象 + * @returns {object} 返回匹配的对象 */ -export default (array, field, value) => { +export default (arrObj, field, value) => { let result = {}; - for (let item of array) { - for (let prop in item) { - if (item.hasOwnProperty(prop)) { + for (const item of arrObj) { + for (const prop in item) { + if (Object.prototype.hasOwnProperty.call(item, prop)) { if (prop === field && item[prop] === value) { result = item; break; diff --git a/lib/array/flatten.js b/lib/array/flatten.js index 991e0c6..dcb0353 100644 --- a/lib/array/flatten.js +++ b/lib/array/flatten.js @@ -2,22 +2,24 @@ * 将多维数组拍平为一维数组 * @author WZBBiao * @category 数组 - * @param {Array} array - 需要拍平的数组 + * @alias yd_array_flatten + * @param {Array} arr - 需要拍平的数组 * @returns {Array} - 拍平后的数组 * @example * console.log(yd_array_flatten([1, [2, [3, [4]], 5]])); // [1, 2, 3, 4, 5] */ -export default (array) => { +export default (arr) => { const result = []; const flatten = (arr) => { arr.forEach((item) => { if (Array.isArray(item)) { flatten(item); - } else { + } + else { result.push(item); } }); - }; - flatten(array); + } + flatten(arr); return result; -}; +} diff --git a/lib/array/groupBy.js b/lib/array/groupBy.js index 5d903e5..f601afe 100644 --- a/lib/array/groupBy.js +++ b/lib/array/groupBy.js @@ -2,9 +2,10 @@ * 数组数据分组 * @author 陈随易 * @category 数组 + * @alias yd_array_groupBy * @param {Array} array 数组 - * @param {String | Function} key 分组的字段或函数 - * @returns {Object} 返回按某字段或函数分组后的对象 + * @param {string | Function} key 分组的字段或函数 + * @returns {object} 返回按某字段或函数分组后的对象 */ export default (array, key) => { const result = new Map(); @@ -13,7 +14,8 @@ export default (array, key) => { if (typeof key === 'string') { groupKey = item[key]; - } else if (typeof key === 'function') { + } + else if (typeof key === 'function') { groupKey = key(item); } diff --git a/lib/array/keyBy.js b/lib/array/keyBy.js index 6302766..7ec3303 100644 --- a/lib/array/keyBy.js +++ b/lib/array/keyBy.js @@ -2,14 +2,15 @@ * 数组按key排序 * @author 陈随易 * @category 数组 + * @alias yd_array_keyBy * @param {Array} array 数组 - * @param {String} field 映射字段 - * @returns {Object} 返回根据字段映射的对象 + * @param {string} field 映射字段 + * @returns {object} 返回根据字段映射的对象 */ export default (array, field) => { const result = {}; array.forEach((item) => { result[item[field]] = item; - }); + }) return result; }; diff --git a/lib/array/paging.js b/lib/array/paging.js index e36b8cb..bf2a927 100644 --- a/lib/array/paging.js +++ b/lib/array/paging.js @@ -2,12 +2,13 @@ * 对数组进行分页 * @author XiaoXinYo * @category 数组 + * @alias yd_array_paging * @param {Array} array 数组 * @param {number} page_size 每页数量 * @returns {Array} 返回已分好的数组 */ export default (array, page_size) => { - let result = []; + const result = [] for (let i = 0; i < array.length; i += page_size) { result.push(array.slice(i, i + page_size)); } diff --git a/lib/array/shuffle.js b/lib/array/shuffle.js index 4c2eb54..ea8ba2e 100644 --- a/lib/array/shuffle.js +++ b/lib/array/shuffle.js @@ -2,6 +2,7 @@ * 数组随机排序(俗称洗牌) * @author 生命过客 <739694218@qq.com> * @category 数组 + * @alias yd_array_shuffle * @param {Array} array 数组 * @returns {Array} 返回随机排序后的数组 */ diff --git a/lib/array/sortBy.js b/lib/array/sortBy.js index 6f23f2b..ca80cf0 100644 --- a/lib/array/sortBy.js +++ b/lib/array/sortBy.js @@ -1,6 +1,6 @@ // >>> HELPERS <<< -const castComparer = (comparer) => (a, b, order) => comparer(a, b, order) * order; +const castComparer = comparer => (a, b, order) => comparer(a, b, order) * order; const unpackObjectSorter = function (sortByObj) { const { asc, desc } = sortByObj || {}; @@ -22,10 +22,12 @@ const multiPropertySorterProvider = function (defaultComparer) { if (typeof sortBy === 'string') { valA = a[sortBy]; valB = b[sortBy]; - } else if (typeof sortBy === 'function') { + } + else if (typeof sortBy === 'function') { valA = sortBy(a); valB = sortBy(b); - } else { + } + else { const objectSorterConfig = unpackObjectSorter(sortBy); return multiPropertySorter(objectSorterConfig.sortBy, sortByArr, depth, objectSorterConfig.order, objectSorterConfig.comparer || defaultComparer, a, b); } @@ -38,7 +40,7 @@ const multiPropertySorterProvider = function (defaultComparer) { return equality; }; -}; +} function getSortStrategy(sortBy, comparer, order) { // Flat array sorter @@ -95,29 +97,34 @@ function createNewSortInstance(opts) { }, by(sortBy) { return sortArray(1, arrayToSort.slice(), sortBy, comparer); - } - }; + }, + } }; } -const defaultComparer = (a, b, order) => { - if (a == null) return order; - if (b == null) return -order; +function defaultComparer(a, b, order) { + if (a == null) + return order; + if (b == null) + return -order; if (typeof a !== typeof b) { if (typeof a < typeof b) { return -1; - } else { + } + else { return 1; } } - if (a < b) return -1; - if (a > b) return 1; + if (a < b) + return -1; + if (a > b) + return 1; return 0; -}; +} export default createNewSortInstance({ - comparer: defaultComparer -}); + comparer: defaultComparer, +}) diff --git a/lib/array/sumBy.js b/lib/array/sumBy.js index 7611846..b8a4714 100644 --- a/lib/array/sumBy.js +++ b/lib/array/sumBy.js @@ -2,14 +2,15 @@ * 数组求和 * @author 陈随易 * @category 数组 - * @param {Array} array 数组数据 + * @alias yd_array_sumBy + * @param {Array} arr 数组数据 * @param {Function} iteratee 迭代函数 - * @returns {Number} 返回求和值 + * @returns {number} 返回求和值 */ -export default (array, iteratee) => { +export default (arr, iteratee) => { let sum = 0; - for (let i = 0; i < array.length; i++) { - sum += iteratee(array[i]); + for (let i = 0; i < arr.length; i++) { + sum += iteratee(arr[i]); } return sum; }; diff --git a/lib/array/uniqWith.js b/lib/array/uniqWith.js index 4a64bf4..75ed50e 100644 --- a/lib/array/uniqWith.js +++ b/lib/array/uniqWith.js @@ -2,6 +2,7 @@ * 数组去重 * @author 陈随易 * @category 数组 + * @alias yd_array_uniqWith * @param {Array} array 数组数据 * @param {Function} comparator 比较函数 * @returns {Array} 返回比较函数去重后的数组 diff --git a/lib/array/unique.js b/lib/array/unique.js index 49340ea..17bbf40 100644 --- a/lib/array/unique.js +++ b/lib/array/unique.js @@ -2,6 +2,7 @@ * 数组去重 * @author 陈随易 * @category 数组 + * @alias yd_array_unique * @param {Array} array 数组数据 * @returns {Array} 返回去重后的数组 */ diff --git a/lib/browser/exitFullScreen.js b/lib/browser/exitFullScreen.js index 8a99922..a1d95ed 100644 --- a/lib/browser/exitFullScreen.js +++ b/lib/browser/exitFullScreen.js @@ -2,15 +2,21 @@ * 退出网页全屏 * @author 陈随易 * @category 浏览器 + * @alias yd_browser_exitFullScreen */ export default () => { - if (document.exitFullscreen) { - document.exitFullscreen(); - } else if (document.msExitFullscreen) { - document.msExitFullscreen(); - } else if (document.mozCancelFullScreen) { - document.mozCancelFullScreen(); - } else if (document.webkitExitFullscreen) { - document.webkitExitFullscreen(); + if (document.fullscreenElement || document.webkitFullscreenElement) { + if (document.exitFullscreen) { + document.exitFullscreen(); + } + else if (document.msExitFullscreen) { + document.msExitFullscreen(); + } + else if (document.mozCancelFullScreen) { + document.mozCancelFullScreen(); + } + else if (document.webkitExitFullscreen) { + document.webkitExitFullscreen(); + } } -}; +} diff --git a/lib/browser/openFullscreen.js b/lib/browser/openFullscreen.js index f1a7b9e..362b88c 100644 --- a/lib/browser/openFullscreen.js +++ b/lib/browser/openFullscreen.js @@ -2,16 +2,20 @@ * 开启网页全屏 * @author 陈随易 * @category 浏览器 - * @param {String} element 元素 + * @alias yd_browser_exitFullScreen + * @param {string} element 元素 */ export default (element) => { if (element.requestFullscreen) { element.requestFullscreen(); - } else if (element.mozRequestFullScreen) { + } + else if (element.mozRequestFullScreen) { element.mozRequestFullScreen(); - } else if (element.msRequestFullscreen) { + } + else if (element.msRequestFullscreen) { element.msRequestFullscreen(); - } else if (element.webkitRequestFullscreen) { + } + else if (element.webkitRequestFullscreen) { element.webkitRequestFullScreen(); } }; diff --git a/lib/browser/viewTransition.js b/lib/browser/viewTransition.js index 8765ff9..e722857 100644 --- a/lib/browser/viewTransition.js +++ b/lib/browser/viewTransition.js @@ -2,14 +2,16 @@ * 应用 viewTransition api * @author imddc https://github.com/imddc * @category 浏览器 + * @alias yd_browser_viewTransition * @param {any} fn 用于触发过渡的函数 * @summary 应用场景:使用viewTransition进行视图过渡 */ export default (fn) => { + // eslint-disable-next-line no-undef const isSupport = doucment !== undefined && 'startViewTransition' in document; if (!isSupport) { console.warn('not supported view transition in your broswer'); - return; + return } let promiseResolve; @@ -18,21 +20,22 @@ export default (fn) => { const promise = new Promise((resolve, reject) => { promiseResolve = resolve; promiseReject = reject; - }); + }) const transition = document.startViewTransition(() => { try { fn(); promiseResolve(); - } catch (e) { + } + catch (e) { console.error(e); promiseReject(); } return promise; - }); + }) transition.finished.then(() => { promiseResolve = undefined; promiseReject = undefined; - }); -}; + }) +} diff --git a/lib/browser/denyChange.js b/libPend/denyChange.js similarity index 69% rename from lib/browser/denyChange.js rename to libPend/denyChange.js index 8e617d7..42c26f3 100644 --- a/lib/browser/denyChange.js +++ b/libPend/denyChange.js @@ -1,20 +1,22 @@ /** * 浏览器禁止切换页面 + * @author Nil + * @category 浏览器 + * @alias yd_array_uniqWith * @param {Function} 函数 * @param {number} number 切换次数,默认是3次 - * @param {String} msg 切换次数,默认提示内容“切换次数超过限制” - * @author Nil + * @param {string} msg 切换次数,默认提示内容“切换次数超过限制” * @summary 应用场景:线上考试场景 */ export default (number, msg) => { window.onblur = function () { let i = number || 3; - i--; + i-- if (i == 0) { - let res = msg || '切换次数超过限制'; + const res = msg || '切换次数超过限制' alert(res); } }; return window.onblur; -}; +} diff --git a/lib/browser/download.js b/libPend/download.js similarity index 52% rename from lib/browser/download.js rename to libPend/download.js index a1064a5..1bdb74d 100644 --- a/lib/browser/download.js +++ b/libPend/download.js @@ -1,45 +1,66 @@ /** * 将字符串、画布元素、图像元素、`Blob`或`File`、`ArrayBuffer`或类型化数组、对象(转为JSON字符串)等下载为文件 - * @param {HTMLCanvasElement|HTMLMediaElement|HTMLImageElement|HTMLSourceElement|HTMLTrackElement|HTMLEmbedElement|HTMLObjectElement|File|Blob|ArrayBuffer|URL|String} content 下载内容/下载链接 - * @param {String} [filename] 文件名 默认使用页面标题 - * @param {Boolean} [isURL] 传入的字符串是否为下载链接。(否则将作为文本内容下载) 默认为`false` + * @param {HTMLCanvasElement | HTMLMediaElement | HTMLImageElement | HTMLSourceElement | HTMLTrackElement | HTMLEmbedElement | HTMLObjectElement | File | Blob | ArrayBuffer | URL | string} content 下载内容/下载链接 + * @param {string} [filename] 文件名 默认使用页面标题 + * @param {boolean} [isURL] 传入的字符串是否为下载链接。(否则将作为文本内容下载) 默认为`false` */ export default (content, filename = document.title, isURL = 'false') => { - if (content === void 0 || content === null) throw new TypeError('无下载内容!'); + if (content === void 0 || content === null) + throw new TypeError('无下载内容!'); const anchor = document.createElement('a'); // 将数值类型与布尔类型转为字符串 - if (typeof content === 'number' || typeof content === 'bigint' || typeof content === 'boolean') return yd_download(String(content), filename); + if (typeof content === 'number' || typeof content === 'bigint' || typeof content === 'boolean') + return yd_download(String(content), filename); if (typeof content === 'string') { - if (isURL) anchor.href = content; + if (isURL) + anchor.href = content; // 下载为文本内容 else return yd_download(new Blob([content]), filename); } // 对象类型 else if (typeof content === 'object') { // 画布元素转为base64作为下载链接 - if (content instanceof HTMLCanvasElement) anchor.href = content.toDataURL('image/png'); + if (content instanceof HTMLCanvasElement) { + anchor.href = content.toDataURL('image/png') + } // 包含src属性的元素直接使用src属性作为下载链接 - else if (content instanceof HTMLImageElement || content instanceof HTMLMediaElement || content instanceof HTMLSourceElement || content instanceof HTMLTrackElement || content instanceof HTMLEmbedElement) anchor.href = content.src; + else if (content instanceof HTMLImageElement || content instanceof HTMLMediaElement || content instanceof HTMLSourceElement || content instanceof HTMLTrackElement || content instanceof HTMLEmbedElement) { + anchor.href = content.src + } // object元素使用data属性作为下载链接 - else if (content instanceof HTMLObjectElement) anchor.href = content.data; + else if (content instanceof HTMLObjectElement) { + anchor.href = content.data + } // File和Blob类型转为url作为下载链接 - else if (content instanceof Blob) anchor.href = URL.createObjectURL(content); + else if (content instanceof Blob) { + anchor.href = URL.createObjectURL(content) + } // ArrayBuffer将转为Blob进行下载 - else if (content instanceof ArrayBuffer) return yd_download(new Blob([content]), filename); + else if (content instanceof ArrayBuffer) { + return yd_download(new Blob([content]), filename) + } // URL对象转为字符串形式 - else if (content instanceof URL) anchor.href = content.href; + else if (content instanceof URL) { + anchor.href = content.href + } // 类型化数组与DataView 将转为ArrayBuffer - else if (ArrayBuffer.isView(content)) return yd_download(content.buffer, filename); + else if (ArrayBuffer.isView(content)) { + return yd_download(content.buffer, filename) + } else { // 尝试转为JSON字符串 try { return yd_download(new Blob([JSON.stringify(content)]), filename); - } catch { + } + catch { throw new TypeError('对象可能包含循环引用。'); } } - } else throw new TypeError('无法处理的类型'); + } + else { + throw new TypeError('无法处理的类型') + } // 设置文件名 anchor.download = filename; anchor.click(); -}; +}