0.2.0 • Published 2 years ago

@me5on/fn v0.2.0

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

npm.io

FN

fun fun fun

A small utility library for dealing with functions

Examples

K

const konst = K('this is the value', 'following args are ignored');

console.log(
  konst.name,                  // K(this is the value)
  konst.toString(),            // () => $
  konst(),                     // this is the value
  konst('different value'),    // this is the value
);

ident

const a = [0, 1, 2, 3, 4];

console.log(
  a.map(ident),                     // [ 0, 1, 2, 3, 4 ]
  a.filter(ident),                  // [ 1, 2, 3, 4 ]
);


const b = [Symbol(1), Symbol(2)];
const c = b.map(ident);

console.log(
  b.map((_, i) => b[i] === c[i]),  // [ true, true ]
);

pipe and compose

const plus10 = $ => $ + 10;
const times2 = $ => $ * 2;


const plusBeforeTimes = pipe(plus10, times2);       // ($ + 10) * 2
const plusAfterTimes = compose(plus10, times2);     // ($ * 2) + 10

console.log(
  plusBeforeTimes.name,                             // pipe(plus10,times2)
  plusAfterTimes.name,                              // compose(plus10,times2)
);

console.log(
  plusBeforeTimes(1),                               // 22
  plusAfterTimes(1),                                // 12
);

tie

const tie = FN.tie;


const a = (one, two, three) => one + two + three;
const b = function c(...$$) {
  console.log('args provided:', ...$$);
  return $$;
};

class D {
}

const f = tie(a, 1);
const g = tie(b, 'first', Symbol('second'));
const h = tie(f, 2);

const C = tie(D, 1, 2, 3);

console.log(
  f.name,     // a(1)
  g.name,     // c(first,Symbol(second))
  h.name,     // a(1)(2)
  C.name,     // D(1,2,3)
);

console.log(
  f(3),        // NaN
  h(3),        // 6
  new C(),     // D {}
);

g();            // args provided: first Symbol(second)
0.2.0

2 years ago

0.1.0

2 years ago

0.0.5

2 years ago

0.0.6

2 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago