Skip to content

Array & Object Utilities Installation ​

Three packages cover Laravel's Arr class and data_* helpers: @tolki/arr for arrays, @tolki/obj for plain objects, and @tolki/data, which picks between the two at runtime. You can install any of them via npm, yarn, or pnpm:

bash
# npm
npm install @tolki/arr

# pnpm
pnpm add @tolki/arr

# yarn
yarn add @tolki/arr
bash
# npm
npm install @tolki/obj

# pnpm
pnpm add @tolki/obj

# yarn
yarn add @tolki/obj
bash
# npm
npm install @tolki/data

# pnpm
pnpm add @tolki/data

# yarn
yarn add @tolki/data

Choosing a package ​

The packages share almost all of their helpers, so the choice comes down to what your data looks like. @tolki/data depends on the other two, so installing it also installs them.

PackageWorks onFunction namesPick it when
@tolki/arrArraysget, first, pluckYour data is always a list.
@tolki/objPlain objectsget, first, pluckYour data is always a keyed record.
@tolki/dataArrays, plain objects and Maps, decided per calldataGet, dataFirst, dataPluckThe shape is only known at runtime, as with parsed JSON or API data.

@tolki/arr and @tolki/obj use the same names for the helpers they share, so switching between them usually means changing the import. A few exist in only one or two packages: flattenDot is only in @tolki/obj, and count, search, before and after are only in @tolki/data. @tolki/data prefixes its names with data, so they can sit next to either package without a clash. Differences from Laravel lists what the three packages leave out, rename or add.

How @tolki/data decides ​

Every data* helper looks at its first argument and forwards the call:

  • An array goes to the matching @tolki/arr helper. So does anything else that is a list of values, such as a Set or a generator.
  • A plain object or a Map goes to the matching @tolki/obj helper.

The return types follow the same split. dataGet is typed as the two packages' get types combined, so TypeScript answers with the array result for an array and the object result for an object.

Importing the Package ​

You can import individual functions or the package as a namespace. Both tree-shake the same way. On the docs site, each tab below imports from its own package and checks one small call:

js
import { get } from "@tolki/arr";

get([{ price: 100 }], "0.price");
// => 100
js
import { get } from "@tolki/obj";

get({ desk: { price: 100 } }, "desk.price");
// => 100
js
import { dataGet } from "@tolki/data";

dataGet([{ price: 100 }], "0.price");
// => 100

dataGet({ desk: { price: 100 } }, "desk.price");
// => 100

Importing the package as a namespace, as in import * as Arr from "@tolki/arr", tree-shakes the same way when you read its members by name, such as Arr.get(...): your bundle holds only the members you use. Passing the namespace object around, or reading a computed member such as Arr[name], keeps every function. See Bundling and Tree-Shaking.

Bundling ​

A helper call whose result nothing uses drops out of your bundle in Vite (Rolldown), Rollup, webpack and Rspack, because the helpers are marked as free of side effects. This holds for the data* helpers as it does for @tolki/arr and @tolki/obj, including a call at the top level of a module.

esbuild doesn't follow that mark across files, so it keeps the call unless the call site says it is safe to remove (see Bundling and Tree-Shaking):

typescript
export const prices = /* @__PURE__ */ dataPluck(products, "price");

pop, shift, unshift and splice, and dataPop, dataShift, dataUnshift and dataSplice, can change the data you pass, so a call to them is always kept.

How to read the utilities list ​

The utilities list has one section per function. Each section has one tab per package that exports it, in the order @tolki/arr, @tolki/obj, @tolki/data. A function that only some packages have shows only those tabs.

On the docs site, the tabs follow your choice. Pick @tolki/obj once and every section on the page, and the install and import examples above, switch to it. Your pick is remembered the next time you visit.

More documentation ​

  • Dot Notation explains the "user.address.city" paths that get, set, pluck and others read, and where a * works.
  • PHP Semantics explains the PHP rules the packages keep: loose comparison, truthiness, key casting and the exceptions they throw.
  • Differences from Laravel lists where the packages return a value instead of changing a reference, behave differently, leave a function out, rename it or add one.

Released under the MIT License.