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

oneself

v0.0.3

Published

(un)curryThis to avoid [].slice.call, obj.fn.bind(obj), that=this

Downloads

9

Readme

Oneself

Build Status

Generic Function

Oneself is a way to build generic functions: functions that take this as the first parameter.

So instead of writing:

Array.prototype.slice(arguments, 1);

you can create a generic function:

var slice = oneself(Array.prototype.slice);
...
slice(arguments, 1)

As a convenenience, oneself provides all Array methods as generic functions under oneself.array, along with functions in string, date, regexp and object.

var slice = oneself.array.slice;
...
slice(arguments, 1)

var map = oneself.array.map,
    getFullYear = oneself.date.getFullYear;
listEqual(
  map([new Date(2011, 3, 15), new Date(2012, 5, 25)], getFullYear),
  [2011, 2012]);

If desired, you can shim Array methods and some implementations already do this.

oneself.array.shim();
oneself.string.shim();

listEqual(
  Array.map(['eB', 'CF'], String.toLowerCase),
  ['eb', 'cf']);

As shown above, generic functions are useful to map methods over collections of objects. oneself also provides placeholder functions that allow you to map methods with specified arguments. This are named the same as the methods, with a _ suffix.

listEqual(
  map([/(.)/, /g$/, /o/, /o$/], oneself.regexp.test_('dog')),
  [true, true, true, false]);

Objects

You can create generic functions from any object.

  function Point(x, y){
    this.x = x;
    this.y = y;
  }

  function point(x, y){
    return new Point(x, y);
  }

  Point.prototype.toString = function(){
    return '('+this.x+', '+this.y+')';
  };

  Point.prototype.describe = function(){
    return 'Point '+this.toString();
  };

  Point.prototype.add = function(x, y){
    return new Point(this.x + x, this.y+y);
  };

  var p1 = point(1,2);


  var describePoint = oneself(Point.prototype.describe);
  equal(describePoint(p1), p1.describe());

As well as placeholder functions:

var add1_1 = oneself(Point.prototype.add)._(1,1);
equals(add1_1(p1), point(2, 3));

Although it might be better to shim your objects. This will create generic functions for all enumerable methods along with toString.

oneself.shim(Point);

listEqual(
  map(map([point(1,2), point(3,4)], Point.add_(10, 20)), Point.toString),
  ['(11, 22)', '(13, 24)']);

(un)curryThis

Making a generic function from a prototype is also called uncurryThis, described here.

uncurryThis is also available under oneself.uncurry and the companion curryThis is called oneself.curry.

curryThis can be used to avoid that = this:

  var x = {
    b: 'hi',
    c: function(w){
      return this.b+' '+w+'!';
    },
    a: oneself.curry(function(self, cb){
      setTimeout(function(){
        cb(self.b);
      }, 100);
    })
  };

  x.a(function(b){});