attach-args

1.0.2 • Public • Published

attach-args

A simple little utility which attaches arguments to a constructor as class properties.

A prime example:

/**
 * Both classes accomplish the same task of
 * attaching constructor arguments as class properties
 */

class oldWay {
  constructor(someService, someString, someBool) {
    this.someService     = someService
    this.someString      = someString
    this.someBool        = someBool
  }
}

class newWay {
  constructor(someService, someString, someBool) {
    attachArgs(arguments).to(this)
  }
}

Installation

Install as dependency

npm i --save attach-args

Include script in your html

<script src="node_modules/attach-args/dist/attachArgs.min.js"></script>

Or simply require it in Node.js

const attachArgs = require('attach-args')

Supported Class Types

attach-args supports class syntax for:

Usage

Native ES6

class shiny {
  constructor(someString, someOtherString, someService) {

    // this is will attach arguments to this constructor
    // as named properties of the class instance
    attachArgs(arguments).to(this)

    this.someString === 'good'            // true
    this.someOtherString === 'bye'        // true
    this.someService.hello() === 'hello'  // true
  }
}

let say = {
  hello: () => 'hello'
}

let classInstance = new shiny('good', 'bye', say)

Node.js Class

const attachArgs = require('attach-args')

class shiny {
  constructor(someString, someOtherString) {

    attachArgs(arguments).to(this)

    this.someString === 'node.js'            // true
    this.someOtherString === 'ecmascript'    // true
  }
}

let classInstance = new shiny('node.js', 'ecmascript')

Babel 6 Class

If using Babel to transpile your ES6 code, use the toBabelClass method instead of toClass.

class shiny {
  constructor(a, b, c) {

    // this is will set this.a, this.b, & this.c
    attachArgs(arguments).toBabelClass(this)

    this.a === 'hello' // true

    this.verifyItWorked() // true
  }

  verifyItWorked() {
    return this.b === 'small'
  }
}

let classInstance = new shiny('hello', 'small', 'world')

After transpiling, this method parses the arguments to the constructor of the transpiled Babel 6 class.

Contribute

Be sure to check the Contributor Goals section for currently open project tasks.

  1. Run npm i
  2. Run npm develop (this runs a watch & auto-build task)
  3. Make your edits
  4. Run tests (node test-suite.js)
  5. Send Pull Request

Contributor Goals

  • [ ] Add support for single config object rather than arguments
  • [ ] Support Traceur compiled class

Warning

This utility was created to help smooth the niche cases in which multiple arguments to a class cannot be avoided.

However, most of the time, a class receiving a lot of arguments can be a red flag. The single responsibility principle might be of particular help.

Please use your best engineering judgement when authoring JavaScript classes.

See "The Principles of Good Programming" for general advice on avoiding poor coding practices.

Stay Classy, San Diego

Readme

Keywords

none

Package Sidebar

Install

npm i attach-args

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • afaqurk