@utilify/array
Advanced tools
+1
-1
@@ -1,1 +0,1 @@ | ||
| "use strict";function t(t){return[...new Set(t)]}exports.after=function(t,r){return t.slice(r+1)},exports.before=function(t,r){return t.slice(0,r)},exports.chunk=function(t,r){const n=[];for(let e=0;e<t.length;e+=r)n.push(t.slice(e,e+r));return n},exports.difference=function(t,r){return t.filter((t=>{if(!r.includes(t))return t}))},exports.first=function(t,r=1){return t.slice(0,r)},exports.flattenArr=function(t,r=1/0){return t.flat(r)},exports.getRandom=function(t){return t[Math.floor(Math.random()*t.length)]},exports.isIterable=function(t){return null!=t&&"function"==typeof t?.[Symbol.iterator]},exports.last=function(t,r=1){return t.slice(-r)},exports.rotate=function(t,r){const n=t.length,e=(r%n+n)%n;return t.slice(e).concat(t.slice(0,e))},exports.sanitizeArr=function(t,r,n){const e=[];for(const o of t)r.includes(o)?void 0!==n&&e.push(n):e.push(o);return e},exports.shuffle=function(t){const r=Array.from(t);for(let t=r.length-1;t>0;t--){const n=Math.floor(Math.random()*(t+1));[r[t],r[n]]=[r[n],r[t]]}return r},exports.swap=function(t,r,n){const e=Array.from(t);return[e[r],e[n]]=[e[n],e[r]],e},exports.union=function(...r){return t(r.reduce(((t,r)=>t=[...t,...r]),[]))},exports.unique=t; | ||
| "use strict";function t(t){return[...new Set(t)]}exports.after=function(t,n){return t.slice(n+1)},exports.before=function(t,n){return t.slice(0,n)},exports.chunk=function(t,n){const e=[];for(let r=0;r<t.length;r+=n)e.push(t.slice(r,r+n));return e},exports.compact=function(t){return t.filter((t=>![void 0,null,!1,NaN,0,0n,""].includes(t)))},exports.difference=function(t,n){return t.filter((t=>!n.includes(t)))},exports.first=function(t,n=1){return t.slice(0,n)},exports.flattenArr=function(t,n=1/0){return t.flat(n)},exports.getRandom=function(t){return t[Math.floor(Math.random()*t.length)]},exports.isIterable=function(t){return null!=t&&"function"==typeof t?.[Symbol.iterator]},exports.last=function(t,n=1){return t.slice(-n)},exports.rotate=function(t,n){const e=t.length;return n=(n%e+e)%e,t.slice(n).concat(t.slice(0,n))},exports.sanitizeArr=function(t,n,e){const r=[];for(const o of t)n.includes(o)?void 0!==e&&r.push(e):r.push(o);return r},exports.shuffle=function(t){for(let n=t.length-1;n>0;n--){const e=Math.floor(Math.random()*(n+1));[t[n],t[e]]=[t[e],t[n]]}return t},exports.swap=function(t,n,e){return[t[n],t[e]]=[t[e],t[n]],t},exports.union=function(...n){return t(n.reduce(((t,n)=>[...t,...n]),[]))},exports.unique=t; |
+13
-7
@@ -0,17 +1,21 @@ | ||
| declare function after(str: string, index: number): string; | ||
| declare function after<T>(arr: T[], index: number): T[]; | ||
| declare function before(str: string, index: number): string; | ||
| declare function before<T>(arr: T[], index: number): T[]; | ||
| declare function chunk(arr: string, size: number): string[]; | ||
| declare function chunk(str: string, size: number): string[]; | ||
| declare function chunk<T>(arr: T[], size: number): T[][]; | ||
| declare function compact<T>(arr: T[]): T[]; | ||
| declare function flattenArr<T>(arr: T[], depth?: number): FlatArray<T[], number>[]; | ||
| declare function isIterable(value: unknown): boolean; | ||
| declare function isIterable(value: any): boolean; | ||
| declare function rotate<T>(arr: T[], count: number): T[]; | ||
| declare function rotate<T>(arr: T[], offset: number): T[]; | ||
| declare function shuffle<T>(arr: T[]): T[]; | ||
| declare function swap<T>(arr: T[], pivot: number, partner: number): T[]; | ||
| declare function swap<T>(arr: T[], fromIndex: number, toIndex: number): T[]; | ||
@@ -22,5 +26,7 @@ declare function unique<T>(arr: T[]): T[]; | ||
| declare function first<T>(arr: T[], index?: number): T[]; | ||
| declare function first(str: string, count?: number): string; | ||
| declare function first<T>(arr: T[], count?: number): T[]; | ||
| declare function last<T>(arr: T[], index?: number): T[]; | ||
| declare function last(str: string, count?: number): string; | ||
| declare function last<T>(arr: T[], count?: number): T[]; | ||
@@ -34,2 +40,2 @@ declare function union<T>(...arrs: T[][]): T[]; | ||
| export { after, before, chunk, difference, first, flattenArr, getRandom, isIterable, last, rotate, sanitizeArr, shuffle, swap, union, unique }; | ||
| export { after, before, chunk, compact, difference, first, flattenArr, getRandom, isIterable, last, rotate, sanitizeArr, shuffle, swap, union, unique }; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| function n(n,t){return n.slice(t+1)}function t(n,t){return n.slice(0,t)}function r(n,t){const r=[];for(let o=0;o<n.length;o+=t)r.push(n.slice(o,o+t));return r}function o(n,t=1/0){return n.flat(t)}function e(n){return null!=n&&"function"==typeof n?.[Symbol.iterator]}function u(n,t){const r=n.length,o=(t%r+r)%r;return n.slice(o).concat(n.slice(0,o))}function c(n){const t=Array.from(n);for(let n=t.length-1;n>0;n--){const r=Math.floor(Math.random()*(n+1));[t[n],t[r]]=[t[r],t[n]]}return t}function i(n,t,r){const o=Array.from(n);return[o[t],o[r]]=[o[r],o[t]],o}function f(n){return[...new Set(n)]}function l(n,t){return n.filter((n=>{if(!t.includes(n))return n}))}function s(n,t=1){return n.slice(0,t)}function a(n,t=1){return n.slice(-t)}function h(...n){return f(n.reduce(((n,t)=>n=[...n,...t]),[]))}function d(n,t,r){const o=[];for(const e of n)t.includes(e)?void 0!==r&&o.push(r):o.push(e);return o}function m(n){return n[Math.floor(Math.random()*n.length)]}export{n as after,t as before,r as chunk,l as difference,s as first,o as flattenArr,m as getRandom,e as isIterable,a as last,u as rotate,d as sanitizeArr,c as shuffle,i as swap,h as union,f as unique}; | ||
| function n(n,t){return n.slice(t+1)}function t(n,t){return n.slice(0,t)}function r(n,t){const r=[];for(let u=0;u<n.length;u+=t)r.push(n.slice(u,u+t));return r}function u(n){return n.filter((n=>![void 0,null,!1,NaN,0,0n,""].includes(n)))}function e(n,t=1/0){return n.flat(t)}function o(n){return null!=n&&"function"==typeof n?.[Symbol.iterator]}function c(n,t){const r=n.length;return t=(t%r+r)%r,n.slice(t).concat(n.slice(0,t))}function i(n){for(let t=n.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[n[t],n[r]]=[n[r],n[t]]}return n}function f(n,t,r){return[n[t],n[r]]=[n[r],n[t]],n}function l(n){return[...new Set(n)]}function s(n,t){return n.filter((n=>!t.includes(n)))}function h(n,t=1){return n.slice(0,t)}function a(n,t=1){return n.slice(-t)}function d(...n){return l(n.reduce(((n,t)=>[...n,...t]),[]))}function p(n,t,r){const u=[];for(const e of n)t.includes(e)?void 0!==r&&u.push(r):u.push(e);return u}function g(n){return n[Math.floor(Math.random()*n.length)]}export{n as after,t as before,r as chunk,u as compact,s as difference,h as first,e as flattenArr,g as getRandom,o as isIterable,a as last,c as rotate,p as sanitizeArr,i as shuffle,f as swap,d as union,l as unique}; |
+15
-11
| { | ||
| "name": "@utilify/array", | ||
| "version": "1.0.4", | ||
| "version": "2.0.0", | ||
| "description": "A collection of utility functions for array manipulation and transformation. Simplifies common operations like chunking, flattening, shuffling, finding differences, and more.", | ||
@@ -10,2 +10,3 @@ "keywords": [ | ||
| "chunk", | ||
| "compact", | ||
| "difference", | ||
@@ -55,5 +56,5 @@ "first", | ||
| "@babel/core": "^7.26.0", | ||
| "@babel/preset-env": "^7.26.0", | ||
| "@babel/preset-typescript": "^7.26.0", | ||
| "rollup": "^4.27.4", | ||
| "@babel/preset-env": "^7.26.0", | ||
| "@babel/preset-typescript": "^7.26.0", | ||
| "@eslint/js": "^9.16.0", | ||
| "@rollup/plugin-babel": "^6.0.4", | ||
@@ -63,14 +64,17 @@ "@rollup/plugin-node-resolve": "^16.0.0", | ||
| "@rollup/plugin-typescript": "^12.1.2", | ||
| "rollup-plugin-dts": "^6.1.1", | ||
| "typescript-eslint": "^8.16.0", | ||
| "@types/jest": "^29.5.14", | ||
| "eslint": "^9.16.0", | ||
| "eslint-config-prettier": "^9.1.0", | ||
| "@eslint/js": "^9.16.0", | ||
| "eslint-config-prettier": "^9.1.0", | ||
| "globals": "^15.12.0", | ||
| "jest": "^29.7.0", | ||
| "prettier": "3.4.1", | ||
| "rollup": "^4.27.4", | ||
| "rollup-plugin-dts": "^6.1.1", | ||
| "ts-node": "^10.9.2", | ||
| "tslib": "^2.8.1", | ||
| "jest": "^29.7.0", | ||
| "@types/jest": "^29.5.14", | ||
| "globals": "^15.12.0" | ||
| "typescript-eslint": "^8.16.0" | ||
| }, | ||
| "dependencies": { | ||
| "@utilify/types": "^1.0.3" | ||
| } | ||
| } |
+130
-87
@@ -1,22 +0,22 @@ | ||
| # Array Utilities | ||
| # Array | ||
| **Array utility functions** provide a variety of methods for manipulating and transforming arrays. They help simplify common operations, such as splitting, flattening, shuffling, and finding differences between arrays, among others. | ||
| The **array utility functions** provide a variety of methods to manipulate and transform arrays and strings. They help simplify common operations such as splitting, flattening, shuffling, and finding differences between arrays, among others. | ||
| ## Installation | ||
| To install the array utility functions, use one of the following commands, depending on your package manager: | ||
| To install the array utility functions, use one of the following commands depending on your package manager: | ||
| ```bash [npm] | ||
| ```bash | ||
| npm install @utilify/array | ||
| ``` | ||
| ```bash [yarn] | ||
| ```bash | ||
| yarn add @utilify/array | ||
| ``` | ||
| ```bash [pnpm] | ||
| ```bash | ||
| pnpm add @utilify/array | ||
| ``` | ||
| Once installed, you can import the functions into your project, using either ESM or CJS. | ||
| After installation, you can import the functions into your project using ESM or CJS. | ||
@@ -27,102 +27,145 @@ ## Usage | ||
| ```typescript [esm] | ||
| import { shuffle } from '@utilify/array'; | ||
| ```typescript | ||
| import { shuffle } from '@utilify/array'; | ||
| ``` | ||
| ```javascript [cjs] | ||
| const { shuffle } = require('@utilify/array'); | ||
| ```javascript | ||
| const { shuffle } = require('@utilify/array'); | ||
| ``` | ||
| ## Overview | ||
| Here's an overview of the functions available in the array and string utilities package: | ||
| ### [after](https://utilify-docs.vercel.app/docs/array/after) | ||
| ```typescript | ||
| function after(str: string, index: number): string; | ||
| function after<T>(arr: T[], index: number): T[]; | ||
| ``` | ||
| ## Overview | ||
| Returns the elements of the array or string after the index `index`. | ||
| Here’s an overview of the functions available in the array utility package: | ||
| ### [before](https://utilify-docs.vercel.app/docs/array/before) | ||
| ### [after](./after.md) | ||
| ```typescript | ||
| function after(array: any[], n: number): any[] | ||
| ``` | ||
| Returns the elements of the array after the index `n`. | ||
| ```typescript | ||
| function before(str: string, index: number): string; | ||
| function before<T>(arr: T[], index: number): T[]; | ||
| ``` | ||
| ### [before](./before.md) | ||
| ```typescript | ||
| function before(array: any[], n: number): any[] | ||
| ``` | ||
| Returns the elements of the array before the index `n`. | ||
| Returns the elements of the array or string before the index `index`. | ||
| ### [chunk](./chunk.md) | ||
| ```typescript | ||
| function chunk(array: any[], size: number): any[][] | ||
| ``` | ||
| Divides the array into smaller chunks of size `size`. | ||
| ### [chunk](https://utilify-docs.vercel.app/docs/array/chunk) | ||
| ### [difference](./difference.md) | ||
| ```typescript | ||
| function difference(array: any[], values: any[]): any[] | ||
| ``` | ||
| Returns the elements of the array that are not present in the second array. | ||
| ```typescript | ||
| function chunk(str: string, size: number): string[]; | ||
| function chunk<T>(arr: T[], size: number): T[][]; | ||
| ``` | ||
| ### [first](./first.md) | ||
| ```typescript | ||
| function first(array: any[], n?: number): any[] | ||
| ``` | ||
| Returns the first `n` elements of the array. | ||
| Splits the array or string into smaller chunks of size `size`. | ||
| ### [flattenArr](./flattenArr.md) | ||
| ```typescript | ||
| function flattenArr(array: any[], depth?: number): any[] | ||
| ``` | ||
| Flattens the array to the specified depth. | ||
| ### [compact](https://utilify-docs.vercel.app/docs/array/compact) | ||
| ### [getRandom](./getRandom.md) | ||
| ```typescript | ||
| function getRandom(array: any[]): any | ||
| ``` | ||
| Returns a random element from the array. | ||
| ```typescript | ||
| function compact<T>(arr: T[]): T[]; | ||
| ``` | ||
| ### [isIterable](./isIterable.md) | ||
| ```typescript | ||
| function isIterable(obj: any): boolean | ||
| ``` | ||
| Checks if the object is iterable. | ||
| Removes "falsy" values (`false`, `null`, `0`, `""`, `undefined`, `NaN`) from the array. | ||
| ### [last](./last.md) | ||
| ```typescript | ||
| function last(array: any[], n?: number): any[] | ||
| ``` | ||
| Returns the last `n` elements of the array. | ||
| ### [difference](https://utilify-docs.vercel.app/docs/array/difference) | ||
| ### [rotate](./rotate.md) | ||
| ```typescript | ||
| function rotate(array: any[], n: number): any[] | ||
| ``` | ||
| Rotates the elements of the array by `n` positions. | ||
| ```typescript | ||
| function difference<T>(arr1: T[], arr2: T[]): T[]; | ||
| ``` | ||
| ### [sanitizeArr](./sanitizeArr.md) | ||
| ```typescript | ||
| function sanitizeArr(array: any[], values: any[], replaceValue?: any): any[] | ||
| ``` | ||
| Removes or replaces specified values in the array. | ||
| Returns the elements from the first array that are not in the second array. | ||
| ### [shuffle](./shuffle.md) | ||
| ```typescript | ||
| function shuffle(array: any[]): any[] | ||
| ``` | ||
| Shuffles the elements of the array. | ||
| ### [first](https://utilify-docs.vercel.app/docs/array/first) | ||
| ### [swap](./swap.md) | ||
| ```typescript | ||
| function swap(array: any[], index1: number, index2: number): any[] | ||
| ``` | ||
| Swaps the elements at positions `index1` and `index2`. | ||
| ```typescript | ||
| function first(str: string, count?: number): string; | ||
| function first<T>(arr: T[], count?: number): T[]; | ||
| ``` | ||
| ### [union](./union.md) | ||
| ```typescript | ||
| function union(...arrays: any[][]): any[] | ||
| ``` | ||
| Returns the union of multiple arrays. | ||
| Returns the first `count` elements of the array or string. | ||
| ### [unique](./unique.md) | ||
| ```typescript | ||
| function unique(array: any[]): any[] | ||
| ``` | ||
| ### [flattenArr](https://utilify-docs.vercel.app/docs/array/flattenArr) | ||
| ```typescript | ||
| function flattenArr<T>(arr: T[], depth?: number): FlatArray<T[], number>[]; | ||
| ``` | ||
| Flattens the array up to the specified depth. | ||
| ### [getRandom](https://utilify-docs.vercel.app/docs/array/getRandom) | ||
| ```typescript | ||
| function getRandom(str: string): string; | ||
| function getRandom<T>(arr: T[]): T; | ||
| ``` | ||
| Returns a random element from the array or string. | ||
| ### [isIterable](https://utilify-docs.vercel.app/docs/array/isIterable) | ||
| ```typescript | ||
| function isIterable(value: any): boolean; | ||
| ``` | ||
| Checks if the value is iterable. | ||
| ### [last](https://utilify-docs.vercel.app/docs/array/last) | ||
| ```typescript | ||
| function last(str: string, count?: number): string; | ||
| function last<T>(arr: T[], count?: number): T[]; | ||
| ``` | ||
| Returns the last `count` elements of the array or string. | ||
| ### [rotate](https://utilify-docs.vercel.app/docs/array/rotate) | ||
| ```typescript | ||
| function rotate<T>(arr: T[], offset: number): T[]; | ||
| ``` | ||
| Rotates the elements of the array `offset` positions. | ||
| ### [sanitizeArr](https://utilify-docs.vercel.app/docs/array/sanitizeArr) | ||
| ```typescript | ||
| function sanitizeArr<T>(arr: T[], values: T[], replace?: T): T[]; | ||
| ``` | ||
| Removes or replaces the specified values in the array. | ||
| ### [shuffle](https://utilify-docs.vercel.app/docs/array/shuffle) | ||
| ```typescript | ||
| function shuffle<T>(arr: T[]): T[]; | ||
| ``` | ||
| Shuffles the elements of the array. | ||
| ### [swap](https://utilify-docs.vercel.app/docs/array/swap) | ||
| ```typescript | ||
| function swap<T>(arr: T[], fromIndex: number, toIndex: number): T[]; | ||
| ``` | ||
| Swaps the elements at positions `fromIndex` and `toIndex`. | ||
| ### [union](https://utilify-docs.vercel.app/docs/array/union) | ||
| ```typescript | ||
| function union<T>(...arrs: T[][]): T[]; | ||
| ``` | ||
| Returns the union of multiple arrays. | ||
| ### [unique](https://utilify-docs.vercel.app/docs/array/unique) | ||
| ```typescript | ||
| function unique<T>(arr: T[]): T[]; | ||
| ``` | ||
| Returns an array with unique elements. |
11236
15.41%33
17.86%170
33.86%1
Infinity%+ Added
+ Added