UNPKG

ph-utils

Version:

js 开发工具集,前后端都可以使用(commonjs和es module)

213 lines (212 loc) 5.62 kB
/** * 数组排序 * @param arr 待排序数组 * @param order 排序信息, asc - 升序, desc - 倒序 * @param orderKey 如果是个对象数组,按哪个字段排序 * @returns */ export function order(arr, order = "asc", orderKey = null) { const collator = new Intl.Collator(undefined, { numeric: true }); return arr.sort((a, b) => { let aValue = a; let bValue = b; if (typeof a === "object" && orderKey != null) { // @ts-ignore aValue = a[orderKey]; // @ts-ignore bValue = b[orderKey]; } let aGtb = -1; if (typeof aValue === "string") { aGtb = collator.compare(aValue, bValue); } else { aGtb = aValue >= bValue ? 1 : -1; } return order === "asc" ? aGtb : -aGtb; }); } /** * 返回所有集合交集的元素的新集合 */ export function intersection(...arrs) { return arrs.reduce((acc, cur) => { if (cur instanceof Set) { //@ts-ignore if (cur.intersection != null) { //@ts-ignore return acc.intersection(cur); } else { const res = new Set(); for (const value of acc) { if (cur.has(value)) { res.add(value); } } return res; } } else { return acc.filter((x) => cur.includes(x)); } }); } /** * 返回一个包含第一个集合中的元素但不包含后续给定集合中元素的新集合 * @param arrs 新集合 * @returns */ export function difference(...arrs) { return arrs.reduce((acc, cur) => { if (acc instanceof Set) { //@ts-ignore if (acc.difference != null) { //@ts-ignore return acc.difference(cur); } else { const res = new Set(); for (const value of acc) { if (!cur.has(value)) { res.add(value); } } return res; } } else { const res = []; for (const value of acc) { if (!cur.includes(value)) { res.push(value); } } return res; } }); } export function union(...arrs) { return arrs.reduce((acc, cur) => { if (cur instanceof Set) { //@ts-ignore if (cur.union != null) { //@ts-ignore return acc.union(cur); } return new Set([...acc, ...cur]); } else { return [...acc, ...cur]; } }); } /** * 对称差: 返回一个包含此集合或给定集合中的元素的新集合,但不包含同时存在于这两个集合中的元素。 * @param arrs * @returns */ export function symmetricDifference(...arrs) { return arrs.reduce((acc, cur) => { if (cur instanceof Set) { //@ts-ignore if (cur.symmetricDifference != null) { //@ts-ignore return acc.symmetricDifference(cur); } const res = new Set(); for (const item of acc) { if (!cur.has(item)) { res.add(item); } } for (const item of cur) { if (!acc.has(item)) { res.add(item); } } return res; } const res = []; for (const item of acc) { if (!cur.includes(item)) { res.push(item); } } for (const item of cur) { if (!acc.includes(item)) { res.push(item); } } return res; }); } /** * 返回一个布尔值,指示此集合中的所有元素是否都在给定的集合中。 * @param a1 * @param a2 * @returns */ export function isSubsetOf(a1, a2) { //@ts-ignore if (a1 instanceof Set && a1.isSubsetOf != null) { //@ts-ignore return a1.isSubsetOf(a2); } let is = true; for (const item of a1) { if (a2 instanceof Set) { if (!a2.has(item)) { is = false; break; } } else { if (!a2.includes(item)) { is = false; break; } } } return is; } /** * 返回一个布尔值,指示给定集合中的所有元素是否都在此集合中。 * @param arr1 * @param arr2 * @returns */ export function isSupersetOf(arr1, arr2) { return isSubsetOf(arr2, arr1); } /** * 返回一个布尔值,指示此集合是否与给定集合没有公共元素。 * * 判断两个集合是否有交集, 也可以通过 intersection(arr1, arr2).length 判断 * * @param arr1 * @param arr2 * @returns */ export function isDisjointFrom(arr1, arr2) { //@ts-ignore if (arr1 instanceof Set && arr1.isDisjointFrom != null) { //@ts-ignore return arr1.isDisjointFrom(arr2); } let is = true; for (const item of arr1) { if (arr2 instanceof Set) { if (arr2.has(item)) { is = false; break; } } else { if (arr2.includes(item)) { is = false; break; } } } return is; }