mk-pmb / objpop-js

Pop properties from objects and verify you got all of them.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

objpop

Pop properties from objects and verify you got all of them.

Usage

from test/usage.mjs:

import objPop from 'objpop';
function makeSandwich(receipe) {
  const pop = objPop(receipe);
  const sandwich = ('🍞'
    + repeat(pop.ifHas('lettuce', 0), 'πŸ€') // unicode is weak on salad.
    + repeat(pop.ifHas('tomato', 0), 'πŸ…')
    + repeat(pop.ifHas('ham', 0), 'πŸ—')  // close enough.
    + repeat(pop.ifHas('pineapple', 0), '🍍')
    + repeat(pop.ifHas('cheese', 0), 'πŸ§€')
    + repeat(pop.ifHas('cherry', 0), 'πŸ’')
  );
  pop.expectEmpty('unknown ingredients');
  return sandwich;
}

const hawaii = { ham: 1, pineapple: 1, cheese: 1, cherry: 1 };
assert.equal(makeSandwich(hawaii), 'πŸžπŸ—πŸπŸ§€πŸ’');

// Original object wasn't modified, so it works again:
assert.equal(makeSandwich(hawaii), 'πŸžπŸ—πŸπŸ§€πŸ’');

// Except when you use direct mode (.d):
const directPop = objPop.d(hawaii);
assert.equal(directPop('cherry'), 1);
assert.equal(directPop('cherry'), undefined);

const blt = { bacon: 2, lettuce: 3, tomato: 3 };
assert.throws(() => makeSandwich(blt),
  'LeftoverKeys: unknown ingredients: bacon');
// Unicode would be way better with bacon.

Known issues

  • Needs more/better tests and docs.
  • Starting in v0.2.0, you can no longer pop inherited properties even in direct mode. The former .ifHas method has become the default.

 

License

ISC

About

Pop properties from objects and verify you got all of them.


Languages

Language:JavaScript 100.0%