npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tolki/data

v1.0.4

Published

Utilities for working with various data structures in JavaScript.

Readme

Tolki JS Data Package

This package provides utilities for working with JavaScript objects and arrays in one package.

Documentation

The full documentation for the data utilities can be found at https://tolki.abe.dev.

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:

# 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.

| Package | Works on | Function names | Pick it when | | ---------------------------------------------------------- | ------------------------------------------------ | ----------------------------------- | -------------------------------------------------------------------- | | @tolki/arr | Arrays | get, first, pluck | Your data is always a list. | | @tolki/obj | Plain objects | get, first, pluck | Your data is always a keyed record. | | @tolki/data | Arrays, plain objects and Maps, decided per call | dataGet, dataFirst, dataPluck | The 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:

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):

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.

Tolki Array & Object Utilities List

These utilities work on arrays with @tolki/arr, on plain objects with @tolki/obj, and on either one with @tolki/data. On the docs site, every section has a tab for each package that exports the function, and the page remembers the tab you pick, so the next visit starts on the same one.

Utilities List

add after arrayItem before boolean chunk chunkBy chunkWhile collapse combine contains containsStrict count crossJoin diff diffAssoc diffAssocUsing diffKeys diffKeysUsing diffUsing divide dot every except exceptValues exists filter first flatten flip float forget from get has hasAll hasAny integer intersect intersectAssoc intersectAssocUsing intersectByKeys intersectUsing join keyBy keys last map mapSpread mapWithKeys only onlyValues pad partition pluck pop prepend prependKeysWith pull push query random reject replace replaceRecursive reverse search select set shift shuffle skipUntil skipWhile slice sole some sort sortDesc sortRecursive sortRecursiveDesc splice string take takeUntil takeWhile toCssClasses toCssStyles undot union unshift values where whereNotNull

Utilities Details

add

Add an element using "dot" notation if it doesn't exist. A key that is missing or set to null counts as not existing.

Exported as add from @tolki/arr and @tolki/obj, and as dataAdd from @tolki/data.

Laravel: Arr::add()

import { dataAdd } from "@tolki/data";

dataAdd([1, 2], 2, 3);
// => [1, 2, 3]

dataAdd({ name: "Desk" }, "price", 100);
// => { name: "Desk", price: 100 }

dataAdd({ name: "Desk", price: 100 }, "price", 200);
// => { name: "Desk", price: 100 }

dataAdd({}, "product.price", 100);
// => { product: { price: 100 } }

after

Get the item after a specified value, or null when the value is the last one or is not found.

Exported as dataAfter from @tolki/data.

import { dataAfter } from "@tolki/data";

dataAfter([1, 2, 3], 2);
// => 3

dataAfter({ a: 1, b: 2, c: 3 }, 2);
// => 3

dataAfter([1, 2, 3], 3);
// => null

arrayItem

Get an item using "dot" notation, throwing an InvalidArgumentException unless it is a list (@tolki/arr) or a plain object (@tolki/obj). dataItem expects the kind that matches its first argument.

Exported as arrayItem from @tolki/arr, as objectItem from @tolki/obj and as dataItem from @tolki/data.

Laravel: Arr::array()

import { dataItem } from "@tolki/data";

dataItem([{ tags: ["a", "b"] }], "0.tags");
// => ["a", "b"]

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

dataItem({ desk: { price: 100 } }, "desk.price");
// => throws InvalidArgumentException

before

Get the item before a specified value, or null when the value is the first one or is not found.

Exported as dataBefore from @tolki/data.

import { dataBefore } from "@tolki/data";

dataBefore([1, 2, 3], 2);
// => 1

dataBefore({ a: 1, b: 2, c: 3 }, 2);
// => 1

dataBefore([1, 2, 3], 1);
// => null

boolean

Get a boolean item using "dot" notation, throwing an InvalidArgumentException if the value is not a boolean.

Exported as boolean from @tolki/arr and @tolki/obj, and as dataBoolean from @tolki/data.

Laravel: Arr::boolean()

import { dataBoolean } from "@tolki/data";

dataBoolean({ available: true }, "available");
// => true

dataBoolean([true, false], 1);
// => false

dataBoolean({ available: true }, "missing", false);
// => false

dataBoolean({ name: "Joe" }, "name");
// => throws InvalidArgumentException

chunk

Break an array or object into chunks of the given size.

Exported as chunk from @tolki/arr and @tolki/obj, and as dataChunk from @tolki/data.

import { dataChunk } from "@tolki/data";

dataChunk([1, 2, 3, 4, 5], 2);
// => [[1, 2], [3, 4], [5]]

dataChunk({ a: 1, b: 2, c: 3 }, 2);
// => { 0: { a: 1, b: 2 }, 1: { c: 3 } }

chunkBy

Break into chunks by comparing adjacent values, using a key path or a callback.

Exported as chunkBy from @tolki/arr and @tolki/obj, and as dataChunkBy from @tolki/data.

import { dataChunkBy } from "@tolki/data";

dataChunkBy([1, 1, 2], (value) => value);
// => [[1, 1], [2]]

dataChunkBy({ a: 1, b: 1, c: 2 }, (value) => value);
// => { 0: { a: 1, b: 1 }, 1: { c: 2 } }

chunkWhile

Break into chunks with a callback that decides whether the next value joins the current chunk. It receives the value, its key and the chunk built so far.

Exported as chunkWhile from @tolki/arr and @tolki/obj, and as dataChunkWhile from @tolki/data.

import { dataChunkWhile } from "@tolki/data";

dataChunkWhile([1, 1, 2], (value, index, chunk) => chunk.at(-1) === value);
// => [[1, 1], [2]]

collapse

Collapse an array of arrays into a single array, or an object of objects into a single object.

Exported as collapse from @tolki/arr and @tolki/obj, and as dataCollapse from @tolki/data.

Laravel: Arr::collapse()

import { dataCollapse } from "@tolki/data";

dataCollapse([[1, 2], [3]]);
// => [1, 2, 3]

dataCollapse({ a: { x: 1 }, b: { y: 2 } });
// => { x: 1, y: 2 }

combine

Combine two sets: the values of the first become the keys and the values of the second become the values. Both must hold the same number of items.

Exported as combine from @tolki/arr and @tolki/obj, and as dataCombine from @tolki/data.

import { dataCombine } from "@tolki/data";

dataCombine(["name", "price"], ["Desk", 100]);
// => { name: "Desk", price: 100 }

contains

Determine if there is a given value, a matching item or a matching key path. The comparison is loose, so "1" matches 1; see PHP Semantics.

Exported as contains from @tolki/arr and @tolki/obj, and as dataContains from @tolki/data.

import { dataContains } from "@tolki/data";

dataContains([1, 2, 3], 2);
// => true

dataContains({ a: 1, b: 2 }, (value) => value > 1);
// => true

containsStrict

Determine if there is a given value, compared strictly, so "02" does not match 2.

Exported as containsStrict from @tolki/arr and @tolki/obj, and as dataContainsStrict from @tolki/data.

import { dataContainsStrict } from "@tolki/data";

dataContainsStrict([1, 3, 5, "02"], 2);
// => false

dataContainsStrict([{ user: { id: 2 } }], "user.id", 2);
// => true

count

Count the number of items in an array, an object or a Map.

Exported as dataCount from @tolki/data.

import { dataCount } from "@tolki/data";

dataCount([1, 2, 3]);
// => 3

dataCount({ a: 1, b: 2 });
// => 2

crossJoin

Cross join the given sets, returning every possible combination.

Exported as crossJoin from @tolki/arr and @tolki/obj, and as dataCrossJoin from @tolki/data.

Laravel: Arr::crossJoin()

import { dataCrossJoin } from "@tolki/data";

dataCrossJoin([1, 2], ["a", "b"]);
// => [[1, "a"], [1, "b"], [2, "a"], [2, "b"]]

diff

Get the items whose values are not present in the other set.

Exported as diff from @tolki/arr and @tolki/obj, and as dataDiff from @tolki/data.

import { dataDiff } from "@tolki/data";

dataDiff([1, 2, 3, 4], [2, 4]);
// => [1, 3]

dataDiff({ a: 1, b: 2, c: 3 }, { x: 2 });
// => { a: 1, c: 3 }

diffAssoc

Get the items whose key and value are not both present in the other set.

Exported as diffAssoc from @tolki/arr and @tolki/obj, and as dataDiffAssoc from @tolki/data.

import { dataDiffAssoc } from "@tolki/data";

dataDiffAssoc({ a: 1, b: 2, c: 3 }, { b: 2 });
// => { a: 1, c: 3 }

diffAssocUsing

Get the items whose key and value are not both present in the other set, using a callback to say whether two keys match.

Exported as diffAssocUsing from @tolki/arr and @tolki/obj, and as dataDiffAssocUsing from @tolki/data.

import { dataDiffAssocUsing } from "@tolki/data";

const same = (a, b) => a.toLowerCase() === b.toLowerCase();

dataDiffAssocUsing({ a: "green", b: "brown" }, { A: "green", c: "blue" }, same);
// => { b: "brown" }

diffKeys

Get the items whose key is not present in the other set. Values are ignored.

Exported as diffKeys from @tolki/arr and @tolki/obj, and as dataDiffKeys from @tolki/data.

import { dataDiffKeys } from "@tolki/data";

dataDiffKeys([1, 2, 3], [9, 9]);
// => [3]

dataDiffKeys({ id: 1, first_word: "Hello" }, { id: 123, foo_bar: "Hello" });
// => { first_word: "Hello" }

diffKeysUsing

Get the items whose key is not present in the other set, using a callback to say whether two keys match. Values are ignored.

Exported as diffKeysUsing from @tolki/arr and @tolki/obj, and as dataDiffKeysUsing from @tolki/data.

import { dataDiffKeysUsing } from "@tolki/data";

const same = (a, b) => a.toLowerCase() === b.toLowerCase();

dataDiffKeysUsing(
  { id: 1, first_word: "Hello" },
  { ID: 123, foo_bar: "Hello" },
  same,
);
// => { first_word: "Hello" }

diffUsing

Get the items whose values are not present in the other set, using a callback to say whether two values are equal.

Exported as diffUsing from @tolki/arr and @tolki/obj, and as dataDiffUsing from @tolki/data.

import { dataDiffUsing } from "@tolki/data";

const same = (a, b) => a.toLowerCase() === b.toLowerCase();

dataDiffUsing(["Green", "Brown", "Blue"], ["GREEN", "Yellow"], same);
// => ["Brown", "Blue"]

divide

Divide into two lists: one with the keys and the other with the values.

Exported as divide from @tolki/arr and @tolki/obj, and as dataDivide from @tolki/data.

Laravel: Arr::divide()

import { dataDivide } from "@tolki/data";

dataDivide(["Desk", 100]);
// => [[0, 1], ["Desk", 100]]

dataDivide({ name: "Desk", price: 100 });
// => [["name", "price"], ["Desk", 100]]

dot

Flatten a multi-dimensional array or object into a single level that uses "dot" notation to show depth. See Dot Notation. The second argument is a key prefix, usually "". The third is a depth: how many levels to join into dotted keys. A depth of 0 or null joins none, as in Laravel. A Map in the data is flattened in its key order.

Exported as dot from @tolki/arr and @tolki/obj, and as dataDot from @tolki/data.

Laravel: Arr::dot()

import { dataDot } from "@tolki/data";

dataDot({ products: { desk: { price: 100 } } });
// => { "products.desk.price": 100 }

dataDot(["a", ["b", "c"]]);
// => { 0: "a", "1.0": "b", "1.1": "c" }

every

Determine if all items pass the given truth test.

Exported as every from @tolki/arr and @tolki/obj, and as dataEvery from @tolki/data.

Laravel: Arr::every()

import { dataEvery } from "@tolki/data";

dataEvery([1, 2, 3], (value) => value > 0);
// => true

dataEvery({ a: 1, b: 2 }, (value) => value > 1);
// => false

except

Get all of the given array or object except for a specified list of keys.

Exported as except from @tolki/arr and @tolki/obj, and as dataExcept from @tolki/data.

Laravel: Arr::except()

import { dataExcept } from "@tolki/data";

dataExcept(["a", "b", "c"], 1);
// => ["a", "c"]

dataExcept({ name: "Desk", price: 100 }, "price");
// => { name: "Desk" }

exceptValues

Get all of the given array or object except for a specified list of values. Values match loosely, unless strict is true.

Exported as exceptValues from @tolki/arr and @tolki/obj, and as dataExceptValues from @tolki/data.

Laravel: Arr::exceptValues()

import { dataExceptValues } from "@tolki/data";

dataExceptValues([1, 2, 3, 2], 2);
// => [1, 3]

dataExceptValues({ a: 1, b: 2, c: 1 }, 1);
// => { b: 2 }

exists

Determine if the given key exists in the provided array or object. A key set to null still exists.

Exported as exists from @tolki/arr and @tolki/obj, and as dataExists from @tolki/data.

Laravel: Arr::exists()

import { dataExists } from "@tolki/data";

dataExists(["a", "b"], 1);
// => true

dataExists({ name: "Desk" }, "sku");
// => false

filter

Filter the array or object using a callback. Without a callback, it removes the items that PHP treats as empty. See Truthiness.

Exported as filter from @tolki/arr and @tolki/obj, and as dataFilter from @tolki/data.

import { dataFilter } from "@tolki/data";

dataFilter([1, 2, 3, 4], (value) => value > 2);
// => [3, 4]

dataFilter({ a: 1, b: 2, c: 3 }, (value) => value > 1);
// => { b: 2, c: 3 }

first

Get the first item, or the first item that passes a truth test. When nothing is found, it returns the default value, or null.

Exported as first from @tolki/arr and @tolki/obj, and as dataFirst from @tolki/data.

Laravel: Arr::first()

import { dataFirst } from "@tolki/data";

dataFirst([1, 2, 3, 4], (value) => value > 2);
// => 3

dataFirst({ a: 1, b: 2, c: 3 }, (value) => value > 1);
// => 2

dataFirst([], null, "none");
// => "none"

function* naturals() {
  for (let n = 1; ; n++) {
    yield n;
  }
}

dataFirst(naturals(), (value) => value > 3);
// => 4

flatten

Flatten a multi-dimensional array or object into a single-level array of its values. The depth argument limits how many levels to flatten.

Exported as flatten from @tolki/arr and @tolki/obj, and as dataFlatten from @tolki/data.

Laravel: Arr::flatten()

import { dataFlatten } from "@tolki/data";

dataFlatten([1, [2, [3]]]);
// => [1, 2, 3]

dataFlatten({ a: 1, b: { c: 2 } });
// => [1, 2]

flip

Flip the keys and the values: each value becomes a key, and each key becomes its value.

Exported as flip from @tolki/arr and @tolki/obj, and as dataFlip from @tolki/data.

import { dataFlip } from "@tolki/data";

dataFlip(["a", "b", "c"]);
// => { a: 0, b: 1, c: 2 }

dataFlip({ name: "taylor" });
// => { taylor: "name" }

float

Get a float item using "dot" notation, throwing an InvalidArgumentException if the value is not a number.

Exported as float from @tolki/arr and @tolki/obj, and as dataFloat from @tolki/data.

Laravel: Arr::float()

import { dataFloat } from "@tolki/data";

dataFloat({ product: { price: 19.99 } }, "product.price");
// => 19.99

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

float accepts a whole number such as 1, where Laravel's Arr::float() throws, because PHP's is_float() rejects an integer. JavaScript has one number type. See Behaviour differences.

forget

Remove one or many items from an array or object using "dot" notation. It returns a new value and leaves the original unchanged. See Return values instead of references.

Exported as forget from @tolki/arr and @tolki/obj, and as dataForget from @tolki/data.

Laravel: Arr::forget() · data_forget()

import { dataForget } from "@tolki/data";

dataForget({ user: { name: "John", age: 30 } }, "user.age");
// => { user: { name: "John" } }

dataForget(["a", "b", "c"], 1);
// => ["a", "c"]

from

Get the underlying array or object of items from the given argument: a list stays a list, and a Map becomes an object. A scalar throws an InvalidArgumentException.

Exported as from from @tolki/arr and @tolki/obj, and as dataFrom from @tolki/data.

Laravel: Arr::from()

import { dataFrom } from "@tolki/data";

dataFrom([1, 2]);
// => [1, 2]

dataFrom(new Map([["foo", "bar"]]));
// => { foo: "bar" }

get

Get an item from an array or object using "dot" notation. A missing path returns the default value, or null. See Dot Notation.

Exported as get from @tolki/arr and @tolki/obj, and as dataGet from @tolki/data.

Laravel: Arr::get() · data_get()

import { dataGet } from "@tolki/data";

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

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

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

has

Determine if an item or items exist in an array or object using "dot" notation. A key set to null still exists. When given several keys, all of them must exist. See Dot Notation.

Exported as has from @tolki/arr and @tolki/obj, and as dataHas from @tolki/data.

Laravel: Arr::has()

import { dataHas } from "@tolki/data";

dataHas([["a"], ["b"]], "0.0");
// => true

dataHas({ name: "John", address: { city: "NYC" } }, ["name", "address.city"]);
// => true

hasAll

Determine if all keys exist in an array or object using "dot" notation. See Dot Notation.

Exported as hasAll from @tolki/arr and @tolki/obj, and as dataHasAll from @tolki/data.

Laravel: Arr::hasAll()

import { dataHasAll } from "@tolki/data";

dataHasAll([["a"], ["b"]], ["0.0", "1.0"]);
// => true

dataHasAll({ name: "John" }, ["name", "email"]);
// => false

hasAny

Determine if any of the keys exist in an array or object using "dot" notation. See Dot Notation.

Exported as hasAny from @tolki/arr and @tolki/obj, and as dataHasAny from @tolki/data.

Laravel: Arr::hasAny()

import { dataHasAny } from "@tolki/data";

dataHasAny([["a"], ["b"]], ["0.5", "1.0"]);
// => true

dataHasAny({ name: "John" }, ["email", "phone"]);
// => false

integer

Get an integer item using "dot" notation, throwing an InvalidArgumentException if the value is not an integer.

Exported as integer from @tolki/arr and @tolki/obj, and as dataInteger from @tolki/data.

Laravel: Arr::integer()

import { dataInteger } from "@tolki/data";

dataInteger({ user: { age: 30 } }, "user.age");
// => 30

dataInteger([{ age: 30 }], "0.age");
// => 30

intersect

Get the items whose values are also present in the other set. Values are compared as strings, so 2 matches "2". The keys of the kept items stay with them in an object.

Exported as intersect from @tolki/arr and @tolki/obj, and as dataIntersect from @tolki/data.

import { dataIntersect } from "@tolki/data";

dataIntersect(["a", "b", "c"], ["b", "c", "d"]);
// => ["b", "c"]

dataIntersect({ a: "green", b: "brown" }, { x: "green" });
// => { a: "green" }

intersectAssoc

Get the items whose key and value are both present in the other set.

Exported as intersectAssoc from @tolki/arr and @tolki/obj, and as dataIntersectAssoc from @tolki/data.

import { dataIntersectAssoc } from "@tolki/data";

dataIntersectAssoc({ a: "green", b: "brown" }, { a: "green", b: "red" });
// => { a: "green" }

intersectAssocUsing

Get the items whose key and value are both present in the other set, using a callback to say whether two keys match.

Exported as intersectAssocUsing from @tolki/arr and @tolki/obj, and as dataIntersectAssocUsing from @tolki/data.

import { dataIntersectAssocUsing } from "@tolki/data";

const same = (a, b) => a.toLowerCase() === b.toLowerCase();

dataIntersectAssocUsing({ a: "green", b: "brown" }, { A: "green" }, same);
// => { a: "green" }

intersectByKeys

Get the items whose key is present in the other set. Values are ignored.

Exported as intersectByKeys from @tolki/arr and @tolki/obj, and as dataIntersectByKeys from @tolki/data.

import { dataIntersectByKeys } from "@tolki/data";

dataIntersectByKeys({ a: 1, b: 2, c: 3 }, { a: 0, c: 0 });
// => { a: 1, c: 3 }

intersectUsing

Get the items whose values are present in the other set, using a callback to say whether two values are equal.

Exported as intersectUsing from @tolki/arr and @tolki/obj, and as dataIntersectUsing from @tolki/data.

import { dataIntersectUsing } from "@tolki/data";

const same = (a, b) => a.toLowerCase() === b.toLowerCase();

dataIntersectUsing(["Green", "Brown"], ["GREEN"], same);
// => ["Green"]

join

Join all items using a string. The final item can use a separate glue string.

Exported as join from @tolki/arr and @tolki/obj, and as dataJoin from @tolki/data.

Laravel: Arr::join()

import { dataJoin } from "@tolki/data";

dataJoin(["a", "b", "c"], ", ", " and ");
// => "a, b and c"

dataJoin({ a: "x", b: "y" }, ", ", " and ");
// => "x and y"

keyBy

Key an array or object of rows by a field, or by the result of a callback.

Exported as keyBy from @tolki/arr and @tolki/obj, and as dataKeyBy from @tolki/data.

Laravel: Arr::keyBy()

import { dataKeyBy } from "@tolki/data";

dataKeyBy(
  [
    { id: 1, name: "Desk" },
    { id: 2, name: "Chair" },
  ],
  "id",
);
// => { 1: { id: 1, name: "Desk" }, 2: { id: 2, name: "Chair" } }

keys

Get all of the keys of an array or object.

Exported as keys from @tolki/arr and @tolki/obj, and as dataKeys from @tolki/data.

import { dataKeys } from "@tolki/data";

dataKeys(["a", "b"]);
// => [0, 1]

dataKeys({ name: "Desk", price: 100 });
// => ["name", "price"]

last

Get the last item, or the last item that passes a truth test. When nothing is found, it returns the default value, or null.

Exported as last from @tolki/arr and @tolki/obj, and as dataLast from @tolki/data.

Laravel: Arr::last()

import { dataLast } from "@tolki/data";

dataLast([1, 2, 3], (value) => value < 3);
// => 2

dataLast({ a: 1, b: 2, c: 3 });
// => 3

map

Run a map over each of the items in an array or object. The callback gets the value, then the index or key.

Exported as map from @tolki/arr and @tolki/obj, and as dataMap from @tolki/data.

Laravel: Arr::map()

import { dataMap } from "@tolki/data";

dataMap([1, 2, 3], (value) => value * 2);
// => [2, 4, 6]

dataMap({ a: 1, b: 2 }, (value) => value * 2);
// => { a: 2, b: 4 }

mapSpread

Run a map over each row, spreading the row's items into the callback's arguments, followed by the index or key.

Exported as mapSpread from @tolki/arr and @tolki/obj, and as dataMapSpread from @tolki/data.

Laravel: Arr::mapSpread()

import { dataMapSpread } from "@tolki/data";

dataMapSpread(
  [
    [1, 2],
    [3, 4],
  ],
  (a, b) => a + b,
);
// => [3, 7]

mapWithKeys

Run an associative map over each of the items. The callback returns an object whose pairs become the entries of the result.

Exported as mapWithKeys from @tolki/arr and @tolki/obj, and as dataMapWithKeys from @tolki/data.

Laravel: Arr::mapWithKeys()

import { dataMapWithKeys } from "@tolki/data";

dataMapWithKeys(["a", "b"], (value, index) => ({ [value]: index }));
// => { a: 0, b: 1 }

only

Get a subset of the items from the given array or object. The result keeps the order of the data, not the order of the keys.

Exported as only from @tolki/arr and @tolki/obj, and as dataOnly from @tolki/data.

Laravel: Arr::only()

import { dataOnly } from "@tolki/data";

dataOnly({ a: 1, b: 2, c: 3 }, ["a", "c"]);
// => { a: 1, c: 3 }

dataOnly(["a", "b", "c"], [0, 2]);
// => ["a", "c"]

onlyValues

Get a subset of the items from the given array or object by value. The comparison is loose, so "1" matches 1; pass true as the third argument to compare strictly. See PHP Semantics.

Exported as onlyValues from @tolki/arr and @tolki/obj, and as dataOnlyValues from @tolki/data.

Laravel: Arr::onlyValues()

import { dataOnlyValues } from "@tolki/data";

dataOnlyValues({ name: "taylor", age: 26 }, [26]);
// => { age: 26 }

dataOnlyValues(["foo", "bar", "baz"], ["foo", "baz"]);
// => ["foo", "baz"]

pad

Pad an array or object to the specified length with a value. A positive size pads at the end, and a negative size pads at the start.

Exported as pad from @tolki/arr and @tolki/obj, and as dataPad from @tolki/data.

import { dataPad } from "@tolki/data";

dataPad([1, 2], 4, 0);
// => [1, 2, 0, 0]

dataPad({ a: 1, b: 2 }, 4, 0);
// => { 0: 0, 1: 0, a: 1, b: 2 }

partition

Partition an array or object into two using the given callback. The result is a pair: the items that pass, then the items that fail.

Exported as partition from @tolki/arr and @tolki/obj, and as dataPartition from @tolki/data.

Laravel: Arr::partition()

import { dataPartition } from "@tolki/data";

dataPartition([1, 2, 3, 4], (value) => value > 2);
// => [[3, 4], [1, 2]]

dataPartition({ a: 1, b: 2, c: 3 }, (value) => value > 1);
// => [{ b: 2, c: 3 }, { a: 1 }]

pluck

Pluck an array of values from an array or object of rows. The path can be a dot path, and a * segment collects every item at that step; see Wildcards.

Exported as pluck from @tolki/arr and @tolki/obj, and as dataPluck from @tolki/data.

Laravel: Arr::pluck()

import { dataPluck } from "@tolki/data";

dataPluck([{ name: "John" }, { name: "Jane" }], "name");
// => ["John", "Jane"]

dataPluck({ a: { name: "John" }, b: { name: "Jane" } }, "name");
// => ["John", "Jane"]

pop

Get and remove the last N items from an array or object. It changes the data you pass in, unlike most functions on this page. See Return values instead of references.

Exported as pop from @tolki/arr and @tolki/obj, and as dataPop from @tolki/data.

import { dataPop } from "@tolki/data";

const stack = [1, 2, 3];

dataPop(stack);
// => 3

stack;
// => [1, 2]

prepend

Push an item onto the beginning of an array or object. It returns a new value and leaves the original unchanged.

Exported as prepend from @tolki/arr and @tolki/obj, and as dataPrepend from @tolki/data.

Laravel: Arr::prepend()

import { dataPrepend } from "@tolki/data";

dataPrepend([2, 3], 1);
// => [1, 2, 3]

dataPrepend({ b: 2, c: 3 }, 1, "a");
// => { a: 1, b: 2, c: 3 }

prependKeysWith

Prepend the key names of an array or object with the given string.

Exported as prependKeysWith from @tolki/arr and @tolki/obj, and as dataPrependKeysWith from @tolki/data.

Laravel: Arr::prependKeysWith()

import { dataPrependKeysWith } from "@tolki/data";

dataPrependKeysWith({ a: 1, b: 2 }, "item_");
// => { item_a: 1, item_b: 2 }

pull

Get a value from an array or object, and remove it. It returns { value, data }, where data is what is left, and it leaves the original unchanged. See Return values instead of references.

Exported as pull from @tolki/arr and @tolki/obj, and as dataPull from @tolki/data.

Laravel: Arr::pull()

import { dataPull } from "@tolki/data";

dataPull({ a: 1, b: 2 }, "b");
// => { value: 2, data: { a: 1 } }

dataPull(["a", "b", "c"], 1);
// => { value: "b", data: ["a", "c"] }

push

Push one or more items into the array at the given key, using "dot" notation. It returns a new value and leaves the original unchanged. See Return values instead of references. When the target is an object or a Map, the new item goes under the next whole-number key, as in Laravel.

Exported as push from @tolki/arr and @tolki/obj, and as dataPush from @tolki/data.

Laravel: Arr::push()

import { dataPush } from "@tolki/data";

dataPush({ a: [1, 2] }, "a", 3, 4);
// => { a: [1, 2, 3, 4] }

dataPush([1, 2], null, 3, 4);
// => [1, 2, 3, 4]

query

Convert an array or object into a URL-encoded query string.

Exported as query from @tolki/arr and @tolki/obj, and as dataQuery from @tolki/data.

Laravel: Arr::query()

import { dataQuery } from "@tolki/data";

dataQuery({ name: "John", age: 30 });
// => "name=John&age=30"

dataQuery(["a", "b"]);
// => "0=a&1=b"

random

Get one or a specified number of random values from an array or object. The picked items come back in the order they have in the data.

Exported as random from @tolki/arr and @tolki/obj, and as dataRandom from @tolki/data.

Laravel: Arr::random()

import { dataRandom } from "@tolki/data";

dataRandom([1, 2, 3, 4], 2);
// e.g. [2, 4]

dataRandom({ a: 1, b: 2, c: 3 }, 1);
// e.g. { 0: 2 }

dataRandom([1, 2, 3, 4], 3).length;
// => 3

reject

Filter an array or object using the negation of the given callback: the items that fail the test stay.

Exported as reject from @tolki/arr and @tolki/obj, and as dataReject from @tolki/data.

Laravel: Arr::reject()

import { dataReject } from "@tolki/data";

dataReject([1, 2, 3], (value) => value > 1);
// => [1]

dataReject({ a: 1, b: 2, c: 3 }, (value) => value > 1);
// => { a: 1 }

replace

Replace the items of an array or object with the given replacer items. It returns a new value. Only the top level is replaced; see replaceRecursive for nested values.

Exported as replace from @tolki/arr and @tolki/obj, and as dataReplace from @tolki/data.

import { dataReplace } from "@tolki/data";

dataReplace({ a: 1, b: 2 }, { b: 3, c: 4 });
// => { a: 1, b: 3, c: 4 }

dataReplace([1, 2, 3], [9]);
// => [9, 2, 3]

replaceRecursive

Recursively replace the items of an array or object with the given items. Two nested arrays or objects merge, and any other value is replaced. It returns a new value.

Exported as replaceRecursive from @tolki/arr and @tolki/obj, and as dataReplaceRecursive from @tolki/data.

import { dataReplaceRecursive } from "@tolki/data";

dataReplaceRecursive(
  { user: { name: "John", age: 30 } },
  { user: { age: 31 } },
);
// => { user: { name: "John", age: 31 } }

reverse

Reverse the order of an array or object and return the result.

Exported as reverse from @tolki/arr and @tolki/obj, and as dataReverse from @tolki/data.

import { dataReverse } from "@tolki/data";

dataReverse([1, 2, 3]);
// => [3, 2, 1]

dataReverse({ a: 1, b: 2, c: 3 });
// => { c: 3, b: 2, a: 1 }

search

Search for a value in an array or object and return its key. It returns false when nothing matches. The comparison is loose by default, so "2" matches 2; pass true as the third argument to compare strictly. See PHP Semantics.

Exported as dataSearch from @tolki/data.

import { dataSearch } from "@tolki/data";

dataSearch([1, 2, 3], 2);
// => 1

dataSearch({ a: 1, b: 2 }, 2);
// => "b"

dataSearch([1, 2, 3], "2");
// => 1

dataSearch([1, 2, 3], "2", true);
// => false

dataSearch({ a: 1, b: 5 }, (value) => value > 2);
// => "b"

select

Select an array or object of values from each item in an array or object. Each key is an exact name, not a dot path.

Exported as select from @tolki/arr and @tolki/obj, and as dataSelect from @tolki/data.

Laravel: Arr::select()

import { dataSelect } from "@tolki/data";

dataSelect([{ a: 1, b: 2, c: 3 }], ["a", "c"]);
// => [{ a: 1, c: 3 }]

set

Set an array or object item to a given value using "dot" notation. It returns a new value and leaves the original unchanged. See Return values instead of references.

Exported as set from @tolki/arr and @tolki/obj, and as dataSet from @tolki/data.

Laravel: Arr::set() · data_set()

import { dataSet } from "@tolki/data";

dataSet({ a: 1 }, "b.c", 3);
// => { a: 1, b: { c: 3 } }

dataSet(["a", "b"], 1, "x");
// => ["a", "x"]

shift

Get and remove the first N items from an array or object. It changes the data you pass in, unlike most functions on this page. See Return values instead of references.

Exported as shift from @tolki/arr and @tolki/obj, and as dataShift from @tolki/data.

import { dataShift } from "@tolki/data";

const queue = [1, 2, 3];

dataShift(queue);
// => 1

queue;
// => [2, 3]

shuffle

Shuffle an array or object and return the result. The result is a new value; an object comes back as a list-like object whose keys are 0 to n - 1.

Exported as shuffle from @tolki/arr and @tolki/obj, and as dataShuffle from @tolki/data.

Laravel: Arr::shuffle()

import { dataShuffle } from "@tolki/data";

dataShuffle([1, 2, 3, 4]);
// e.g. [3, 1, 4, 2]

dataShuffle([1, 2, 3, 4]).length;
// => 4

skipUntil

Skip items in an array or object until the given condition is met, and return the rest. The condition is a value, matched strictly, so "2" does not match 2, or a callback.

Exported as skipUntil from @tolki/arr and @tolki/obj, and as dataSkipUntil from @tolki/data.

import { dataSkipUntil } from "@tolki/data";

dataSkipUntil([1, 2, 3, 4], 3);
// => [3, 4]

dataSkipUntil({ a: 1, b: 2, c: 3 }, (value) => value > 1);
// => { b: 2, c: 3 }

skipWhile

Skip items in an array or object while the given condition is met, and return the rest. The condition is a value, matched strictly, or a callback.

Exported as skipWhile from @tolki/arr and @tolki/obj, and as dataSkipWhile from @tolki/data.

import { dataSkipWhile } from "@tolki/data";

dataSkipWhile([1, 1, 2, 1], 1);
// => [2, 1]

dataSkipWhile({ a: 1, b: 2, c: 3 }, (value) => value < 2);
// => { b: 2, c: 3 }

slice

Get a portion of an array or object, starting at an offset. The original is left unchanged; splice is the function that removes items.

Exported as slice from @tolki/arr and @tolki/obj, and as dataSlice from @tolki/data.

import { dataSlice } from "@tolki/data";

dataSlice([1, 2, 3, 4], 1, 2);
// => [2, 3]

dataSlice({ a: 1, b: 2, c: 3 }, 1);
// => { b: 2, c: 3 }

sole

Get the first item, but only if exactly one item exists. Otherwise it throws ItemNotFoundException when none is found, or MultipleItemsFoundException when several are. With a callback, it counts only the items the callback accepts.

Exported as sole from @tolki/arr and @tolki/obj, and as dataSole from @tolki/data.

Laravel: Arr::sole()

import { dataSole } from "@tolki/data";

dataSole([42]);
// => 42

dataSole({ a: 1, b: 2 }, (value) => value > 1);
// => 2

dataSole([1, 2]);
// => throws MultipleItemsFoundException

some

Determine if some items pass the given truth test.

Exported as some from @tolki/arr and @tolki/obj, and as dataSome from @tolki/data.

Laravel: Arr::some()

import { dataSome } from "@tolki/data";

dataSome([1, 3, 5], (value) => value % 2 === 0);
// => false

dataSome({ a: 1, b: 2 }, (value) => value > 1);
// => true

sort

Sort an array or object by its values, by a callback, by a "dot" path, or by a list of sort descriptors. It returns a new value and leaves the original unchanged.

Exported as sort from @tolki/arr and @tolki/obj, and as dataSort from @tolki/data.

Laravel: Arr::sort()

import { dataSort } from "@tolki/data";

dataSort([3, 1, 2]);
// => [1, 2, 3]

dataSort({ a: 3, b: 1, c: 2 });
// => { b: 1, c: 2, a: 3 }

sortDesc

Sort an array or object in descending order. It takes the same callbacks, paths and descriptors as sort, and returns a new value.

Exported as sortDesc from @tolki/arr and @tolki/obj, and as dataSortDesc from @tolki/data.

Laravel: Arr::sortDesc()

import { dataSortDesc } from "@tolki/data";

dataSortDesc([3, 1, 2]);
// => [3, 2, 1]

dataSortDesc({ a: 3, b: 1, c: 2 });
// => { a: 3, c: 2, b: 1 }

sortRecursive

Recursively sort an array or object by its keys and values. Nested lists sort by value and nested objects sort by key.

Exported as sortRecursive from @tolki/arr and @tolki/obj, and as dataSortRecursive from @tolki/data.

Laravel: Arr::sortRecursive()

import { dataSortRecursive } from "@tolki/data";

dataSortRecursive({ b: [3, 1, 2], a: { d: 2, c: 1 } });
// => { a: { c: 1, d: 2 }, b: [1, 2, 3] }

dataSortRecursive([
  [3, 1, 2],
  [9, 8],
]);
// => [[8, 9], [1, 2, 3]]

sortRecursiveDesc

Recursively sort an array or object by its keys and values in descending order.

Exported as sortRecursiveDesc from @tolki/arr and @tolki/obj, and as dataSortRecursiveDesc from @tolki/data.

import { dataSortRecursiveDesc } from "@tolki/data";

dataSortRecursiveDesc({ a: [1, 2, 3], b: { c: 1, d: 2 } });
// => { b: { d: 2, c: 1 }, a: [3, 2, 1] }

dataSortRecursiveDesc([
  [1, 3, 2],
  [8, 9],
]);
// => [[3, 2, 1], [9, 8]]

splice

Remove a portion of an array or object, optionally put replacements in its place, and return what was removed. It changes the data you pass in, unlike most functions on this page. See Return values instead of references.

Exported as splice from @tolki/arr and @tolki/obj, and as dataSplice from @tolki/data.

import { dataSplice } from "@tolki/data";

const queue = [1, 2, 3, 4, 5];

dataSplice(queue, 1, 2);
// => [2, 3]

queue;
// => [1, 4, 5]

string

Get a string item using "dot" notation, throwing an InvalidArgumentException if the value is not a string.

Exported as string from @tolki/arr and @tolki/obj, and as dataString from @tolki/data.

Laravel: Arr::string()

import { dataString } from "@tolki/data";

dataString({ user: { name: "John" } }, "user.name");
// => "John"

dataString(["a", 1], 0);
// => "a"

dataString(["a", 1], 1);
// => throws InvalidArgumentException

take

Take the first or last N items of an array or object. A negative number takes from the end.

Exported as take from @tolki/arr and @tolki/obj, and as dataTake from @tolki/data.

Laravel: Arr::take()

import { dataTake } from "@tolki/data";

dataTake([1, 2, 3, 4], 2);
// => [1, 2]

dataTake({ a: 1, b: 2, c: 3 }, -1);
// => { c: 3 }

takeUntil

Take items from an array or object until the given condition is met. The condition is a value, matched strictly, or a callback.

Exported as takeUntil from @tolki/arr and @tolki/obj, and as dataTakeUntil from @tolki/data.

import { dataTakeUntil } from "@tolki/data";

dataTakeUntil([1, 2, 3, 4], 3);
// => [1, 2]

dataTakeUntil({ a: 1, b: 2, c: 3 }, (value) => value > 1);
// => { a: 1 }

takeWhile

Take items from an array or object while the given condition is met. The condition is a value, matched strictly, or a callback.

Exported as takeWhile from @tolki/arr and @tolki/obj, and as dataTakeWhile from @tolki/data.

import { dataTakeWhile } from "@tolki/data";

dataTakeWhile([1, 1, 2, 2], 1);
// => [1, 1]

dataTakeWhile({ a: 1, b: 2, c: 3 }, (value) => value < 3);
// => { a: 1, b: 2 }

toCssClasses

Conditionally compile CSS classes into a class list. A plain entry is always kept. A class: condition entry is kept when its condition is true.

Exported as toCssClasses from @tolki/arr and @tolki/obj, and as dataToCssClasses from @tolki/data.

Laravel: Arr::toCssClasses()

import { dataToCssClasses } from "@tolki/data";

dataToCssClasses(["a", "b"]);
// => "a b"

dataToCssClasses({ "p-4": true, hidden: false });
// => "p-4"

toCssStyles

Conditionally compile CSS styles into a style list. Each style ends with a semicolon, and the styles are separated by a space.

Exported as toCssStyles from @tolki/arr and @tolki/obj, and as dataToCssStyles from @tolki/data.

Laravel: Arr::toCssStyles()

import { dataToCssStyles } from "@tolki/data";

dataToCssStyles(["color: red"]);
// => "color: red;"

dataToCssStyles({ "color: red": true, "display: none": false });
// => "color: red;"

undot

Expand a flat object with "dot" keys into a nested array or object.

Exported as undot from @tolki/arr and @tolki/obj, and as dataUndot from @tolki/data.

Laravel: Arr::undot()

import { dataUndot } from "@tolki/data";

dataUndot({ "a.b": 1, "a.c": 2 });
// => { a: { b: 1, c: 2 } }

dataUndot({ 0: "a", "1.0": "b" });
// => { 0: "a", 1: ["b"] }

union

Union arrays or objects into one. An earlier argument wins: a later one only adds the keys the result does not have yet.

Exported as union from @tolki/arr and @tolki/obj, and as dataUnion from @tolki/data.

import { dataUnion } from "@tolki/data";

dataUnion({ a: 1 }, { a: 2, b: 3 });
// => { a: 1, b: 3 }

dataUnion([1, 2], [3, 4, 5]);
// => [1, 2, 5]

unshift

Prepend one or more items to the start of an array or object. It changes the data you pass in, unlike most functions on this page. See Return values instead of references.

Exported as unshift from @tolki/arr and @tolki/obj, and as dataUnshift from @tolki/data.

import { dataUnshift } from "@tolki/data";

const queue = [3];

dataUnshift(queue, 1, 2);
// => [1, 2, 3]

queue;
// => [1, 2, 3]

values

Get all the values of an array or object as a list.

Exported as values from @tolki/arr and @tolki/obj, and as dataValues from @tolki/data.

import { dataValues } from "@tolki/data";

dataValues({ a: 1, b: 2 });
// => [1, 2]

dataValues([1, 2]);
// => [1, 2]

where

Filter an array or object using the given callback. A list comes back renumbered from 0; see Behaviour differences.

Exported as where from @tolki/arr and @tolki/obj, and as dataWhere from @tolki/data.

Laravel: Arr::where()

import { dataWhere } from "@tolki/data";

dataWhere([1, 2, 3], (value) => value > 1);
// => [2, 3]

dataWhere({ a: 1, b: 2, c: 3 }, (value) => value > 1);
// => { b: 2, c: 3 }

whereNotNull

Filter an array or object, removing the items whose value is null. Only null is removed; an undefined item stays. A list comes back renumbered from 0; see Behaviour differences.

Exported as whereNotNull from @tolki/arr and @tolki/obj, and as dataWhereNotNull from @tolki/data.

Laravel: Arr::whereNotNull()

import { dataWhereNotNull } from "@tolki/data";

dataWhereNotNull([1, null, 3]);
// => [1, 3]

dataWhereNotNull({ a: 1, b: null, c: 3 });
// => { a: 1, c: 3 }

dataWhereNotNull([1, undefined, null]);
// => [1, undefined]

Dot Notation

A dot path is a string such as "user.address.city" that reaches into nested arrays and objects in one call. @tolki/arr, @tolki/obj, @tolki/data and the Collection methods all read the same paths.

Path syntax

Each segment between the dots is an object key or an array index. A number segment picks that position of an array. A path that leads nowhere gives the default, which is null when you leave it out.

import { dataGet } from "@tolki/data";

dataGet([{ items: ["a", "b"] }], "0.items.1");
// => "b"

dataGet({ desk: { sizes: ["s", "m"] } }, "desk.color", "none");
// => "none"

Keys that contain a dot

An object key can hold a dot of its own. get from @tolki/obj tries the whole string as a key first, and only splits it into segments when no such key exists.

Laravel's Arr::get() does the same, so the port gives the same answer.

import { get } from "@tolki/obj";

get({ "a.b": 1, a: { b: 2 } }, "a.b");
// => 1

get({ a: { b: 2 } }, "a.b");
// => 2

dataGet from @tolki/data also tries the literal key first, because it forwards to get. Laravel's data_get() does not: it always splits the path, so data_get(['a.b' => 1, 'a' => ['b' => 2]], 'a.b') is 2.

import { dataGet } from "@tolki/data";

dataGet({ "a.b": 1, a: { b: 2 } }, "a.b");
// => 1

Functions that take a path

  • Read: get, has, hasAll, hasAny, and the typed getters string, integer, float, boolean and arrayItem (objectItem, dataItem).
  • Write: set, add, forget, pull and push. set, add and push create the missing steps of a path as they go. forget leaves the data as it is when the path is missing, and pull returns the default, null unless you pass one, with the data unchanged.
  • Pick out of rows: pluck takes a path for the value and for the key, and except takes paths among its keys.
  • Walk: a path goes into lists, plain objects and Maps. A write keeps a Map a Map.
  • Convert: dot turns a nested structure into a flat object of paths, and undot turns it back.
import {
  dataDot,
  dataExcept,
  dataForget,
  dataHas,
  dataInteger,
  dataPull,
  dataPush,
  dataSet,
  dataUndot,
} from "@tolki/data";

dataHas([{ a: 1 }], "0.a");
// => true

dataSet({}, "a.b.c", 1);
// => { a: { b: { c: 1 } } }

dataExcept({ a: { b: 1, c: 2 }, d: 3 }, ["a.b"]);
// => { a: { c: 2 }, d: 3 }

dataInteger({ a: { b: 2 } }, "a.b");
// => 2

dataDot({ a: { b: 1, c: [2] } });
// => { "a.b": 1, "a.c.0": 2 }

dataUndot({ "a.b": 1, "a.c": 2 });
// => { a: { b: 1, c: 2 } }

dataPush({}, "a.b", 1);
// => { a: { b: [1] } }

dataForget({ a: 1 }, "b.c");
// => { a: 1 }

dataPull({ a: 1 }, "b.c");
// => { value: null, data: { a: 1 } }

Wildcards

A * segment stands for every item at that step, and pluck collects what lies behind it. The result has one entry per item in front of the *.

Laravel's Arr::pluck() reads a * the same way, and the port returns the same shape.

import { dataPluck } from "@tolki/data";

const posts = [
  { title: "A", tags: [{ id: 1 }, { id: 2 }] },
  { title: "B", tags: [{ id: 3 }] },
];

dataPluck(posts, "tags.*.id");
// => [[1, 2], [3]]

dataPluck(posts, "tags.*.id", "title");
// => { A: [1, 2], B: [3] }

Where wildcards do not work

Of the functions above, only pluck reads a *. get, has and their data versions take it as the name of a key, so a path with * finds nothing. The same goes for the {first} and {last} segments.

Laravel's data_get() expands both of them. See Differences from Laravel for the full list.

import { dataGet, dataHas } from "@tolki/data";

dataGet({ a: { n: 1 }, b: { n: 2 } }, "*.n");
// => null

dataHas({ a: { n: 1 } }, "*.n");
// => false

dataGet({ s: [{ x: 1 }, { x: 2 }] }, "s.{first}.x");
// => null

Collections read the same paths

@tolki/collection methods that look inside items, such as pluck, where, sortBy, keyBy and sum, take the same paths, and pluck takes the same *. get is the exception: it takes one key, not a path. The method pages are in the Collections section, under Methods List.

import { collect } from "@tolki/collection";

const posts = collect([
  { title: "A", author: { id: 2 }, tags: [{ id: 1 }, { id: 2 }] },
  { title: "B", author: { id: 1 }, tags: [{ id: 3 }] },
]);

posts.pluck("tags.*.id").all();
// => [[1, 2], [3]]

posts.where("author.id", 1).pluck("title").all();
// => ["B"]

posts.sortBy("author.id").pluck("title").all();
// => ["B", "A"]

posts.sum("author.id");
// => 3

collect({ a: { b: 1 } }).get("a.b");
// => null

PHP Semantics

These packages port Laravel, so where JavaScript and PHP disagree about what is equal, what is true or what a key is, the port follows PHP. That keeps a result the same as the one Laravel gives. This page lists the four places it matters. The helpers behind them live in @tolki/utils.

Loose and strict comparison

PHP's == is loose: it converts a value before it compares. A string that reads as a number equals that number, and null equals 0, "" and []. The functions that compare values (contains, search, unique, where and others) use it, and the ones with a Strict twin (containsStrict, whereStrict) use PHP's === instead.

looseEqual from @tolki/utils is the == operator. "1" == 1 and null == 0 are both true in PHP. PHP has no undefined, so the port reads it as null. "abc" does not equal 0, but "1" equals "01". JavaScript's own == gives a different answer for null == 0 (false) and for "1" == "01" (false).

import { looseEqual } from "@tolki/utils";

looseEqual("1", 1);
// => true

looseEqual(null, 0);
// => true

looseEqual(null, undefined);
// => true

looseEqual("abc", 0);
// => false

looseEqual("1", "01");
// => true

The contains and containsStrict pair shows the difference on a list.

import { dataContains, dataContainsStrict } from "@tolki/data";

dataContains([1, 2, 3], "1");
// => true

dataContainsStrict({ a: "1" }, 1);
// => false

On a Collection, where is loose and whereStrict is strict, and where with a == or === operator picks the same two.

import { collect } from "@tolki/collection";

const rows = collect([{ a: 1 }, { a: "1" }, { a: 2 }]);

rows.where("a", 1).all();
// => [{ a: 1 }, { a: "1" }]

rows.whereStrict("a", 1).all();
// => [{ a: 1 }]

rows.where("a", "===", 1).all();
// => [{ a: 1 }]

Truthiness

Wherever a callback or a value is tested for being true, PHP's rule applies, not JavaScript's. The tests are filter() with no callback, Collection's reject() with no argument, Collection's where without an operator, and what a callback to first, where, reject, every, some or sole answers.

In PHP the string "0", the empty string, 0, null, false and the empty array are false. The string "0.0", NaN and a non-empty array are true. The port keeps the same list and adds the JavaScript containers that have no PHP twin: an empty plain object, an empty Map and an empty Set are false. Any other object is true, however empty.

import { collect } from "@tolki/collection";
import { isFalsy, isPhpFalsy } from "@tolki/utils";

collect(["0", "0.0", "", 0, [], {}, NaN, "a", false, null]).filter().all();
// => ["0.0", NaN, "a"]

collect([1, null, 0, 2]).reject().all();
// => [null, 0]

collect([{ a: 1 }, { a: 0 }])
  .where("a")
  .all();
// => [{ a: 1 }]

isPhpFalsy("0");
// => true

isFalsy("0");
// => false

isPhpFalsy(new Map());
// => true

isPhpFalsy(NaN);
// => false

isFalsy is the JavaScript-minded check, where "0" is true. Use isPhpFalsy when you want the rule the packages use.

A callback that answers "0" counts as false, and one that answers "0.0" counts as true.

import { dataWhere } from "@tolki/data";

dataWhere(["a", "b"], () => "0");
// => []

dataWhere({ x: "a", y: "b" }, () => "0.0");
// => { x: "a", y: "b" }

Key casting

PHP stores a string key that reads as a plain integer, such as "10", as the number 10. A string such as "01" stays a string. The port does the same, so a callback gets a number for "10" and a string for "01", and keys lists them that way.

A callback sees the number 1 for the key "1" and a string for "x", and set writes the key "1" as an integer but "01" as a st