ts-left-join
TypeScript icon, indicating that this package has built-in type declarations

2.0.3 • Public • Published

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

Package Sidebar

Install

npm i ts-left-join

Weekly Downloads

6

Version

2.0.3

License

MIT

Unpacked Size

14.4 kB

Total Files

13

Last publish

Collaborators

  • barbarrosa