2.0.3 • Published 4 years ago

ts-left-join v2.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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]
);
2.0.3

4 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago