0.1.3 • Published 3 months ago

@webreflection/partial v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

partial

build

Partial function / Class application.

import { partial, instance } from '@webreflection/partial';

// create a partial function: 3rd argument defaults to 60
const fromCharCode = partial(String.fromCharCode, ...[,,60]);

// pass only 2 args ... 3rd filled automatically
fromCharCode(40, 40);       // "((<"

// if explicitly passed the partial argument is overwritten
fromCharCode(40, 40, 40);   // "((("


const arr = instance(Array, ...[,2,3]);

arr(0);         // [0, 2, 3]
arr(...[1,,4])  // [1, 2, 4]

It is also possible to combine partial with bound function to have max freedom of choice.

import { partial } from '@webreflection/partial';

function xyz(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

const point = {x: 0, y: 0, z: 0};
const updateCoords = partial(xyz.bind(point), ...[,,0]);

updateCoords(1, 2);
point; // {x: 1, y: 2, z: 0}

Alternatively, it is also possible to call or apply explicitly.

import { partial } from '@webreflection/partial';

function xyz(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

const point = {x: 0, y: 0, z: 0};
const updateCoords = partial(xyz, ...[,,0]);

updateCoords.call(point, 1, 2);
point; // {x: 1, y: 2, z: 0}
0.1.3

3 months ago

0.1.2

3 months ago

0.1.0

3 months ago

0.0.0

3 months ago