Skip to content

Typescript-friendly implementation of a left join on arrays

License

Notifications You must be signed in to change notification settings

Barbarrosa/ts-left-join

Repository files navigation

ts-left-join Build Status

Typescript-friendly implementation of a left join on arrays

This library can provide basic left-joins for arrays, but you may want to check out data-forge-ts if you need to do some serious data wrangling. They have a more comprehensive join implementation.

Installation

npm i --save ts-left-join

Usage

import LeftJoin from 'ts-left-join';

const dogs = [
    {name: "Spaz", preference: "tail"},
    {name: "Lone Wolf", preference: "ears"},
];

const cats = [
    {name: "Boing", best_feature: "tail"},
    {name: "Dud", best_feature: "claws"},
];

const best_buds = LeftJoin(
    // left-side field name and array
    "dog",dogs,
    // right-side field name and array
    "cat",cats,
    // join field names (left, right)
    "preference","best_feature"
);

The value best_buds now contains the following.

[
    {
        dog: {name: "Spaz", preference: "tail"},
        cat: {name: "Boing", best_feature: "tail"},
    },
    {
        dog: {name: "Lone Wolf", preference: "ears"},
    },
]

Type Checking

This library uses strict type checking to validate that your fields match your specified data structures. If you don't intend to take advantage of this feature or haven't specified your data structures yet, then you can circumvent the type checking as shown below.

const best_buds = LeftJoin(
    dogName as string,
    dogs,
    // Circumvent field name overlap check
    "cat" as Exclude<string, typeof dogName>,
    cats,
    // Circumvent row field name checks
    "preference" as keyof typeof dogs[any],
    "best_feature" as keyof typeof cats[any]
);

About

Typescript-friendly implementation of a left join on arrays

Resources

License

Stars

Watchers

Forks

Packages

No packages published