npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xo-utils

v2.3.0

Published

Helper utilities for working with functions, arrays and objects

Downloads

12

Readme

Build Status Coverage Status Dependency Status devDependency Status

Helper utils for working with functions, arrays and objects in JavaScript. Full documentation at http://bjdixon.github.io/xo/

Introduction

xo is a library I use to work with functions, arrays and objects in JavaScript.

##Source code The source is available at: http://github.com/bjdixon/xo.

##Installation

####Browser

Download and add to your html pages.

<script type="text/javascript" src="xo.min.js"></script>

####Node

Using npm:

npm install xo-utils
var xo = require('xo-utils');

var curry = require('xo-utils').curry;

##Contains

##Usage

####curry

Takes a function and zero or more arguments to that function. Returns a function that can be invoked with remaining arguments at a later time.

const add = (a, b) => a + b;

const addTen = xo.curry(add, 10);

addTen(32); // => 42

####compose

Takes functions and returns a function. The returned function when invoked will invoke each function that was supplied as an argument to compose (in reverse order) passing the the return value of each as an argument to the next function.

const increment = (a) => a + 1;
const square = (a) => a * a;

const squarePlusOne = xo.compose(increment, square);
squarePlusOne(3); // => 10

####pipe

Takes functions and returns a function. The returned function when invoked will invoke each function that was supplied as an argument to pipe (in the order supplied) passing the the return value of each as an argument to the next function.

const increment = (a) => a + 1;
const square = (a) => a * a;

const plusOneSquare = xo.pipe(increment, square);
plusOneSquare(3); // => 16

####filter

Takes an array and a predicate. Returns an array with only those terms that pass the predicate.

const compare = (id, obj) => id === obj.id;
const objArr = [
  { name: 'a', id: '001' },
  { name: 'b', id: '003' },
  { name: 'c', id: '003' },
  { name: 'd', id: '004' }
];
xo.filter(objArr, xo.curry(compare, '003')); // => [{ name: 'b', id: '003'},{name: 'c', id: '003'}] 

####findIndex

Takes an array and a predicate. Returns the index of the first term that passes the predicate.

const compare = (id, obj) => id === obj.id;
const objArr = [
  { name: 'a', id: '001' },
  { name: 'b', id: '002' },
  { name: 'c', id: '003' },
  { name: 'd', id: '004' }
];
xo.findIndex(objArr, xo.curry(compare, '003')); // => 2

####findKey

Takes an object and a predicate. Returns the key of the first term that passes the predicate.

const compare = (id, obj) => id === obj.id;
const obj = {
  hello: { name: 'a', id: '001' },
  goodbye: { name: 'b', id: '002' },
  yes: { name: 'c', id: '003' },
  no: { name: 'd', id: '004' }
} ;
xo.findKey(obj, xo.curry(compare, '003')); // => 'yes'

####find

Takes an object or an array and a predicate. Returns the value of the first term that passes the predicate.

const compare = (id, obj) => id === obj.id;
const obj = {
  hello: { name: 'a', id: '001' },
  goodbye: { name: 'b', id: '002' },
  yes: { name: 'c', id: '003' },
  no: { name: 'd', id: '004' }
};
const objArr = [
  { name: 'a', id: '001' },
  { name: 'b', id: '002' },
  { name: 'c', id: '003' },
  { name: 'd', id: '004' }
];
xo.find(obj, xo.curry(compare, '003')); // => { name: 'c', id: '003' }
xo.find(objArr, xo.curry(compare, '003')); // => { name: 'c', id: '003' }

####flatten

Takes an n-dimensional nested array. Returns a flattened 1-dimension array.

const test = [0, 1, [2, 3], [4, [5, 6]], 7, [8, [9]]];
xo.flatten(test); // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

####compact

Takes an array. Returns an array with all falsy values removed.

const test = [1, , false, 2, 3, false];
xo.compact(test); // => [1, 2, 3]

####memoize

Takes a function and returns a functions. Invoking the returned function will return cached results if the same arguments have been provided during previous invocations.

const upper = (str) => str.toUpperCase();
const memoUpper = xo.memoize(upper);
memoUpper('foo'); // => "FOO"
memoUpper('foo'); // => "FOO" (cached version)

####isArray

Type check for Array.

xo.isArray([1, 2, 3]); // => true
xo.isArray(true); // => false

####isFunction

Type check for Function.

xo.isFunction(function(){ return true; }); // => true
xo.isFunction(true); // => false

####isObject

Type check for Object.

xo.isObject({ a: true }); // => true
xo.isObject(true); // => false

####isString

Type check for String.

xo.isString('true'); // => true
xo.isString(true); // => false

####isNumber

Type check for Number.

xo.isNumber(42); // => true
xo.isNumber('true'); // => false

####isBoolean

Type check for Boolean.

xo.isBoolean(true); // => true
xo.isBoolean('true'); // => false

####maybe

Takes a function and returns a function. The returned function will not be invoked if supplied with null or undefined arguments.

const sum = (a, b) => a + b;
const maybeSum = xo.maybe(sum);
maybeSum(2, 3); // => 5
maybeSum(null, 3); // doesn't invoke sum

####partial

Takes a function and zero or more arguments to that function. Returns a function that can be invoked with remaining arguments at a later time.

const add = (a, b) => a + b;

const addTen = xo.partial(add, 10);

addTen(32); // => 42

####map

Takes an array and a function. Returns an array that is the result of applying the function to each term of the original array.

const arr = [1, 2, 3, 4];
const square = (a) => a * a;

const out = xo.map(square, arr); // => [1, 4, 9, 16];

####reduce

Takes an array, an initial value and a function. Returns a single value that is the result of applying the function to each term of the array and an accumulator.

const arr = [1, 2, 3, 4];
const sum = (a, b) => a + b;

const out = xo.reduce(sum, 0, arr); // => 10;