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 🙏

© 2026 – Pkg Stats / Ryan Hefner

as-this

v1.2.1

Published

Call an javascript function with `this` referred to the first argument

Readme

as-this

Call a javascript function with this referred to the first argument

Primarily a syntactic sugar for CoffeeScript to replace long variable names with '@':

Install

npm install as-this

Usage

  • as (self, fn)

    call function, return the self object:

    as = require 'as-this'
    self = {}
    ret = as self, ->
      @msg = 'Hello, World'
    console.log self  # => { msg: 'Hello, World' }
    console.log ret  # => { msg: 'Hello, World' }
  • as.call (self, fn)

    get the return value of fn instead of self:

    as = require 'as-this'
    ret = as.call { msg: 'Hello, World' }, ->
      return @msg
    console.log ret  # => 'Hello, World'
  • omits self

    parameter self with be assigned to {} if not specified:

    as = require 'as-this'
    ret = as ->
      @msg = 'Hello, World'
    console.log ret  # => { msg: 'Hello, World' }
  • defer with promise

    return value with be wrapped as promise if fn returns a promise:

    as = require 'as-this'
    promise = as ->
      new Promise (resolve, reject) =>
        @msg = 'Hello, World'
        resolve()
    promise.then (ret) ->
      console.log ret  # => { msg: 'Hello, World' }

    use co-yield to clean your non-blocking code (not available in browser environment):

    co = require 'co'
    as = require 'as-this'
    co ->
      ret = yield as -> co =>
        @msg = 'Hello, World'
      console.log ret  # => { msg: 'Hello, World' }
  • yieldables

    you may use generator function or thunk as fn value, they will be auto wrapped to promise:

    co = require 'co'
    as = require 'as-this'
    co ->
      ret0 = as ->
        @type = 'general'
      ret1 = yield as -> co =>
        @type = 'non-args function returns a promise'
      ret2 = yield as ->
        @type = 'generator function'
        yield []
      ret3 = yield as (cb) ->
        @type = 'thunk'
        cb()
      console.log ret0  # => { type: 'general' }
      console.log ret1  # => { type: 'non-args function returns a promise' }
      console.log ret2  # => { type: 'generator function' }
      console.log ret3  # => { type: 'thunk' }

Scenario

  • create object with interdependent properties

    as = require 'as-this'
    urls = as ->
      @base = "http://host:port"
      @main = "#{@base}/main"
      @list = "#{@base}/list"
      @odd = "#{@list}/odd"
      @even = "#{@list}/even"
      ...

    which is functionally equivalent to:

    urls = do ->
      base = "http://host:port"
      list = "#{base}/list"
      return {
        base: base
        main: "#{base}/main"
        list: list
        odd: "#{list}/odd"
        even: "#{list}/even"
      }
  • do complex configuration on a specified object

    as = require 'as-this'
    ng = require 'angular'
    
    as (ng.module 'myApp', []), ->
    
      @factory 'myServ', ($http) -> as ->
        @load = (url) ->
          $http.get url
        @update = (url, data) ->
          $http.post url, data
    
      @controller 'myController', ($scope, myServ) -> as $scope, ->
        @doRefresh = ->
          myServ.load 'blahblah.json'
            .then (res) =>
              @data = res
        @doRefresh()