Coder Social home page Coder Social logo

obex's Introduction

obex

Transform JavaScript objects.

  • Map and filter objects as easily as arrays
  • Methods can be chained by default
  • 417 bytes gzipped

Installation

npm install --save obex
or
yarn add obex

Usage

var obex = require('obex');

var regularObject = { propertyName: 'value' };
var transformableObject = obex(regularObject);

An obex object is just like a regular JavaScript object, but it can be transformed using methods like filter and map to make object manipulation simpler. These methods can be chained together because obex adds non-enumerable properties to the given raw JavaScript object.

API

obex

obex(object)

  • object: plain old JavaScript object to become transformable using obex methods.
    • Return: obex object

.filter

filter(testFunction)

  • testFunction: function to test each key-value entry in the object.
    • Parameters: (key, value)
    • Return: true to keep the entry, false otherwise.
obex({ a: 3, b: 6 })
   .filter(function(key, value) {
      return value < 5;
   });
// { a : 3 }

.map

map(keyMapper, valueMapper)

  • keyMapper: function to map old keys to new keys.
    • Parameters: (key [, value])
    • Return: replacement key for this entry.
  • valueMapper: function to map old values to new values.
    • Parameters: (value [, key])
    • Return: replacement value for this entry.
obex({ a: 3, b: 6 }).map(
   function(key) {
      return key + key;
   },
   function(value) {
      return value * 2;
   }
);
// { aa: 6, bb: 12 }

.mapKeys

mapKeys(keyMapper)

  • keyMapper: function to map old keys to new keys.
    • Parameters: (key [, value])
    • Return: replacement key for this entry.
obex({ a: 3, b: 6 }).mapKeys(function(key, value) {
   return key + key + value;
});
// { aa3: 3, bb6: 6 }

.mapValues

mapValues(valueMapper)

  • valueMapper: function to map old values to new values.
    • Parameters: (value, [, key])
    • Return: replacement value for this entry.
function square(x) { return x * x };
obex({ a: 3, b: 6 }).mapValues(square);
// { a: 9, b: 36 }

.toArray

toArray(entryMapper)

  • entryMapper: function to map key-value pairs to array elements,
    • Parameters: (key, value)
    • Return: array of elements mapped from key-value pairs
obex({ a: 1, b: 2 }).toArray(function(key, value) {
   return key + value;
});
// ['a1', 'b2']

.keys

keys()
Returns an array containing only the object's keys

obex({ a: 1, b: 2 }).keys();
// ['a', 'b']

.values

values()
Returns an array containing only the object's values

obex({ a: 1, b: 2 }).values();
// [1, 2]

.raw

raw()
Converts back to a plain old JavaScript object.

var a = obex({}).filter().map();
var b = a.raw();
var c = b.filter(); // Error

obex's People

Contributors

wesrage avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.