UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

2 lines (1 loc) 2.12 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function s(n,e){const r=e&&typeof e=="object"?{...e}:e;return n?[r].concat(n.slice(0)):[r]}function g(n,e){const r=e&&typeof e=="object"?{...e}:e;return n?n.slice(0).concat(r):[r]}function p(n,e,r){const t=r&&typeof r=="object"?{...r}:r;return n?[...n.slice(0,e),t,...n.slice(e)]:[t]}function l(n,e,r){if(!n||e===r)return n;const t=[...n],o=t[e];return t.splice(e,1),t.splice(r,0,o),t}function d(n,e){if(!n)return n;const r=n.findIndex(e);return r===-1?n:c(n,r)}function m(n,e){if(!n)return n;const r=n.findIndex(e.finder);return r===-1?n:i(n,r,e.updater)}function c(n,e){return!n||e===-1||e>n.length-1?n:[...n.slice(0,e),...n.slice(e+1)]}function i(n,e,r){if(e===-1||!n||e>n.length-1)return n;const t={...n[e]};r(t);const o=[...n];return o[e]=t,o}function h(n,e){if(!n)return n;const r=new Set,t=new Array;for(const o of n){const u=e?e(o):o;r.has(u)||(r.add(u),t.push(o))}return t}function A(n,e){return n?n.join(e):""}function B(n){return n==null?void 0:n[0]}function S(n,e){if(!n||e<=0)return;const r=Math.ceil(n.length/e);return Array.from({length:r},(t,o)=>n.slice(o*e,(o+1)*e))}function I(n,e){return n.reduce((r,t)=>{const o=e(t);return r[o]||(r[o]=[]),r[o].push(t),r},{})}function v(n,e){const r={};return n.forEach(t=>{const{groupKey:o,groupData:u,child:f}=e(t);r[o]||(r[o]={...u,children:[]}),r[o].children.push(f)}),Object.keys(r).map(t=>r[t])}function b(n,e){const r=[...n];return r.sort(e),r}function j(n,e="ASC"){return function(r,t){const o=n(r),u=n(t);return o<u?e==="ASC"?-1:1:o>u?e==="ASC"?1:-1:0}}const w=(n,e="skeleton_")=>Array.from({length:n}).map((r,t)=>`${e}${t}`);function F(n){return e=>{const t=(n.indexOf(e)+1)%n.length;return n[t]}}exports.arrayFirst=B;exports.arrayJoin=A;exports.arraySplit=S;exports.generateSortByProperty=j;exports.generatorArray=w;exports.generatorArrayToggle=F;exports.groupBy=I;exports.groupTreeBy=v;exports.insert=p;exports.move=l;exports.pull=s;exports.push=g;exports.removeByIndex=c;exports.removeFind=d;exports.sort=b;exports.unique=h;exports.updateByIndex=i;exports.updateFind=m;