MiguelCastillo / belty

General purpose utility belt
MIT License
0 stars 0 forks source link

belty

Greenkeeper badge

General purpose utility belt

as of v5.0.0, belty only generates a minified bundle dist/index.js.

Table of Contents

API

identity(arg?)

Helper method that returns the first argument passed in.

var input = 3.14;
assert(identity(input) === input);

noop

noop method! It takes no arguments and does not return anything. Useful when you need to setup an initial placeholder function.

var input = 3.14;
assert(noop(input) === undefined);

pick(input, keys)

Alias pluck

Method that extracts key value pairs from the input object.

var input = {
  first: "Mgnum",
  last: "Rusty",
  id: "some-random-id"
};

var result = pick(input, ["first", "id", "something that does not exist"]);

// result is:
// {
//   first: "Mgnum",
//   id: "some-random-id"
// }

omit(input, keys)

Pulls out all the items in the input that are not in keys and returns a new object with just that.

Opposite of pick

var input = {
  first: "Mgnum",
  last: "Rusty",
  id: "some-random-id"
};

var result = omit(input, ["first"]);

// result is:
// {
//   last: "Rusty",
//   id: "some-random-id"
// }

assign(target, ...sources, transform)

Alias extend

Shallow copies all properties from the input objects (sources) into the target object. Source objects are processed left to right overriding whatever values already exist in the result.

var input1 = {
  first: "Mgnum",
  last: "Rusty",
  id: "some-random-id"
};

var input2 = {
  address: {
    street: "somewhere st",
    zip: "12345"
  }
};

var result = assign({}, input1, input2);

// result is a shallow copy of the input. So changing anything in address
// in the result will also change input.
//{
//  first: "Mgnum",
//  last: "Rusty",
//  id: "some-random-id",
//  address: {
//    street: "somewhere st",
//    zip: "12345"
//  }
//}

merge(target, ...sources, transform)

Deep copy all properties from the input objects (sources) into the target object. It merges objects and arrays into new structures from left to right overriding all other non array/object properties.

var source1 = {
  data: [1, 2, 3],
  misc: "random"
};

var source2 = {
  data: [4, 5]
};

var result = merge({}, source1, source2);

// {
//   data: [4, 5, 3],
//   misc: "modded"
// }
var source1 = {
  data: [1, 2, 3],
  misc: "random"
};

var source2 = {
  data: [4, 5, 6]
};

var result = merge({}, source1, source2, transform);

function transform(current, next) {
  if (Array.isArray(next.data)) {
    return {
      data: current.data ? current.data.concat(next.data) : next.data
    };
  }

  return next;
}

// The result of this is an object with the array entries concatinated
// and the exapnded out object property as generated by the transform
// method.
// {
//   data: [1, 2, 3, 4, 5, 6],
//   misc: "random"
// }

isMatch(input, criteria)

Deep comparisson of object structures recursively matching all properties in criteria with the input. If everything in the criteria matches the input, then isMatch returns true. Otherwise it returns false.

When matching items in an array, the index position is taken into account.

var input = {
  prop1: 3.14,
  prop2: [4, 8],
  prop3: {
    "prop3--1": [23]
  }
};

// Result is true
isMatch(input, {
  prop1: 3.14
});

// Result is true
isMatch(input, {
  prop2: [4]
});

// Result is false
isMatch(input, {
  prop2: [8]
});

// Result is false
isMatch(input, {
  prop2: [1]
});

// Result is true
isMatch(input, {
  prop3: {
    "prop3--1": [23]
  }
});

find(input, predicate)

Find the first item in the input for which the predicate function returns true for. When the predicate is not a function, isMatch is called with the predicate as the matching criteria.

Predicate functions are called with item, index, and original collection.

Example with an input array

var input = [{
  city: "DET",
  number: 313
}, {
  city: "RO",
  number: 2311
}, {
  city: "DET",
  number: 734
}];

// Result is
// { city: "DET", number: 313 }
findAll(input, {
  city: "DET"
});

Example with an input object

var input = {
  item1: {
    city: "DET",
    number: 313
  },
  item2: {
    city: "RO",
    number: 2311
  },
  item3: {
    city: "DET",
    number: 734
  }
};

// Result is
// { city: "DET", number: 313 }
findAll(input, {
  city: "DET"
});

findAll(input, predicate)

Returns an array with all the items for which the predicate function returns true for. Or in the case when the predicate is not a function, isMatch is called with predicate as the matching criteria.

Predicate functions are called with item, index, and original collection.

Example with an input array

var input = [{
  city: "DET",
  number: 313
}, {
  city: "RO",
  number: 2311
}, {
  city: "DET",
  number: 734
}];

// Result is
// [{ city: "DET", number: 313 }, { city: "DET", number: 734 }]
findAll(input, {
  city: "DET"
});

Example with an input object

var input = {
  item1: {
    city: "DET",
    number: 313
  },
  item2: {
    city: "RO",
    number: 2311
  },
  item3: {
    city: "DET",
    number: 734
  }
};

// Result is
// [{ city: "DET", number: 313 }, { city: "DET", number: 734 }]
findAll(input, {
  city: "DET"
});

value(input, keypath, transform)

Alias objectValue

Extract the value from an input object for the given keypath.

var input = {
  car: {
    interior: {
      seats: {
        count: 2,
        color: "blue"
      }
    }
  }
};

var result = value(input, ["car", "interior", "seats"]);

// result is the seats
// {
//   count: 2,
//   color: "blue"
// }

values(input)

Alias objectValues

Gets the values from a object map and returns them in an array. If an array is passed in, then the array is returned as is.

var input = {
  "foo": "bar",
  "hello": "world"
};

var result = values(input);

// result is an array with just the object values
// ["bar", "world"]

toArray(...)

Converts input items to an array.

When the input is an array, the items in it are added to the final resulting array.

When the input is an object

var input = {
  a: "First value",
  b: "Second value"
};

var result = toArray(input);

// result is an array with the object as its values
[{
  a: "First value",
  b: "Second value"
}]

When the input is an array

var input = [{
  a: "First value",
  b: "Second value"
}];

var result = toArray(input);

// result is an array with the object as its values
[{
  a: "First value",
  b: "Second value"
}]

When the input is one object and an array

var input1 = {
  a: "First value",
  b: "Second value"
};

var input2 = [{
  c: "Third value",
  d: "Fourth value"
}, {
  e: "Fifth value",
  f: "Sixth value"
}];

var result = toArray(input1, input2);

// result is an array with the object as its values
[{
  a: "First value",
  b: "Second value"
}, {
  c: "Third value",
  d: "Fourth value"
}, {
  e: "Fifth value",
  f: "Sixth value"
}]

arrayToObject(input, val)

Converts arrays to a literal objects with the array values as keys. You can optionally pass in a callback function that is called in order to generate the values that go in the final result. val can also just be anything to be used as the value for each entry in the final result, otherwise true is used.

This method is useful in situation where you need to create a lookup table such as an object map (enums).

var input = ["first", "last", "GPS", "location"];
var result = arrayToObject(input);

// result is an object with the array items as the the keys for the object
// {
//   "first": true,
//   "last": true,
//   "GPS": true,
//   "location": true
// }
var input = ["first", "last", "GPS", "location"];
var result = arrayToObject(input, 3.14);

// result is an object with the array items as the the keys for the object
// {
//   "first": 3.14,
//   "last": 3.14,
//   "GPS": 3.14,
//   "location": 3.14
// }
var input = ["first", "last", "GPS", "location"];
var result = arrayToObject(input, transform);

function transform(value, key, array) {
  return value + "-" + key;
}

// result is an object with the array items as the the keys for the object
// {
//   "first": "first-0",
//   "last": "last-1",
//   "GPS": "GPS-2",
//   "location": "location-3"
// }

License

Licensed under MIT