JSPM

  • Created
  • Published
  • Downloads 2178838
  • Score
    100M100P100Q185623F
  • License MIT

Compile time `with` for strict mode JavaScript

Package Exports

  • with

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (with) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

with

Compile time with for strict mode JavaScript

build status Dependency Status NPM version

Installation

$ npm install with

Usage

var addWith = require('with')

addWith('obj', 'console.log(a)')
// => "var a = obj.a;console.log(a)"

addWith("obj || {}", "console.log(helper(a))", ["helper"])
// => var locals = (obj || {}),a = locals.a;console.log(helper(a))

API

addWith(obj, src, [exclude], [environments])

The idea is that this is roughly equivallent to:

with (obj) {
  src
}

There are a few differences though. For starters, it will be assumed that all variables used in src come from obj so any that don't (e.g. template helpers) need to have their names parsed to exclude as an array. If you want to have browser globals available you can pass a collection of environments, e.g.

addWith('obj', 'document.createElement(foo)', [], ['reservedVars', 'ecmaIdentifiers', 'nonstandard', 'browser'])

The default environment is: ['reservedVars', 'ecmaIdentifiers', 'nonstandard', 'node']

It also makes everything be declared, so you can always do:

if (foo === undefined)

instead of

if (typeof foo === 'undefined')

It is also safe to use in strict mode (unlike with) and it minifies properly (with disables virtually all minification).

License

MIT