1.1.1 • Published 3 years ago

@lopatnov/join v1.1.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 years ago

@lopatnov/join Twitter

npm NPM version GitHub issues GitHub forks GitHub stars License

build-and-test-package publish-npm-package Libraries.io dependency status for latest release

Patreon sobe.ru LinkedIn

Object join technics.

Install

https://nodei.co/npm/@lopatnov/join.png?downloads=true&downloadRank=true&stars=true

npm install @lopatnov/join

Browser

<script src="https://lopatnov.github.io/join/dist/join.min.js"></script>

Import package to the project

TypeScript

import { join, JoinTypes } from "@lopatnov/join";

JavaScript

var library = require("@lopatnov/join");
var join = library.join;
var JoinTypes = library.JoinTypes;

Join Types

Join Types

enum JoinTypes {
  none       = 0b0000,
  left       = 0b1000, // take unique left object properties
  right      = 0b0001, // take unique right object properties
  innerLeft  = 0b0100, // take non-unique (inner) properties from left object
  innerRight = 0b0010, // take non-unique (inner) properties from right object
  innerJoin  = none | innerLeft | innerRight | none, // innerLeft + innerRight = deep merge inner join of two objects
  leftJoin   = left | innerLeft | innerRight | none,
  rightJoin  = none | innerLeft | innerRight | right,
  fullJoin   = left | innerLeft | innerRight | right,
  expand     = left | none      | innerRight | right
}

JoinTypes.expand is default join type

How to use

// 1. Set join Type
function join(joinType?: JoinTypes) => (local function)<TContext>(context: TContext)
// 2. Set context (left object)
(local function)<TContext>(context: TContext) => (local function)<TJoinObject>(joinObject: TJoinObject)
// 3. Set join object (right object) and gets result
(local function)<TJoinObject>(joinObject: TJoinObject): TContext & TJoinObject

As three separate operations

Right join sample

const rightJoin = join(JoinTypes.right);

const contextJoinBy = rightJoin({
  sample1: "One",
  sample2: "Two",
  sample3: "Three",
});

const result = contextJoinBy({
  sample2: "Dos",
  sample3: "Tres",
  sample4: "Quatro",
});

console.log(result); // { sample4: "Quatro" }

Left join sample

const leftJoin = join(JoinTypes.left);

const contextJoinBy = leftJoin({
  sample1: "One",
  sample2: "Two",
  sample3: "Three",
});

const result = contextJoinBy({
  sample2: "Dos",
  sample3: "Tres",
  sample4: "Quatro",
});

console.log(result); // { sample1: "One" }

Complex join sample

const complexJoin = join(JoinTypes.left | JoinTypes.innerLeft | JoinTypes.right);

const contextJoinBy = complexJoin({
  sample1: "One",
  sample2: "Two",
  sample3: "Three",
});

const result = contextJoinBy({
  sample2: "Dos",
  sample3: "Tres",
  sample4: "Quatro",
});

console.log(result); // {sample1: "One", sample2: "Two", sample3: "Three", sample4: "Quatro"}

Inner join sample

const result = join(JoinTypes.innerJoin)({
  sample1: "One",
  sample2: "Two",
  sample3: {
    smile: "cheese",
  },
})({
  sample2: "Dos",
  sample3: {
    sorrir: "queijo",
  },
  sample4: "Quatro",
});

console.log(result); // {sample2: "Dos", sample3: {smile: "cheese", sorrir: "queijo"}}

Demo

See, how it's working: https://runkit.com/lopatnov/join

Test it with a runkit: https://npm.runkit.com/@lopatnov/join

Rights and Agreements

License Apache-2.0

Copyright 2020–2021 Oleksandr Lopatnov

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago