# chip-utils

> A few basic utilities for use within chip-supported libraries

Latest version **0.3.0** (published 2017-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install chip-utils
pnpm add chip-utils
yarn add chip-utils
bun add chip-utils
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2017-02-27 |
| First published | 2015-12-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jacob Wright |
| Maintainers | handyccc, jacwright, skylar.pagenkopf |

## Links

- npm: https://www.npmjs.com/package/chip-utils
- Repository: https://github.com/chip-js/chip-utils
- Homepage: http://github.com/chip-js/chip-utils
- Issues: https://github.com/chip-js/chip-utils/issues
- npm.io page: https://npm.io/package/chip-utils

## Recent versions

- 0.3.0 (latest) — 2017-02-27
- 0.2.8 — 2017-01-20
- 0.2.7 — 2017-01-20
- 0.2.6 — 2016-03-04
- 0.2.5 — 2016-01-28
- 0.2.4 — 2016-01-24
- 0.2.3 — 2016-01-19
- 0.2.2 — 2016-01-05
- 0.2.1 — 2015-12-24
- 0.2.0 — 2015-12-24
- 0.1.0 — 2015-12-24

## README

# Chip Utils

Utility methods used in some of the libraries created with Chip.

## Getting Started

To use these utilites in your own project install using npm.

```
npm install chip-utils
```

## Class

This utility provides syntactic sugar on normal JavaScript class inheritance supporting additional features such as
getters/setters and static inheriance.

To set up class inheritance, extend from the generic Class object. Then your classes will have an `extend` method and
can use it to extend to subclasses.

```js
var Class = require('chip-utils/class');

function MyClass() {

}

Class.extend(MyClass);

function SubClass() {

}

MyClass.extend(SubClass);
```

Static properties will also be extended. You may use the `static` member property as a shortcut to defining statics, but
this is not necessary.

```js
// Parent class
function Resource(data) {
  if (data) {
    Object.keys(data).forEach(function(key) {
      this[key] = data[key];
    }, this);
  }
}

Resource.uri = '/api/v1'
Resource.load = function() {
  var ResourceType = this;
  return $.getJSON(this.uri).then(function(items) {
    return items.map(function(item) {
      return new ResourceType(item);
    })
  });
};

Class.extend(Resource, {
  save: function() {
    var uri = this.constructor.uri + '/' + this.id;
    // Save code here
  };
});

function Person(data) {
  Resource.call(this, data);
}

Resource.extend(Person);

// Define this AFTER calling extend writes it
Person.uri += '/people'

Person.load() // returns a promise full of Person objects
```

Using the `static` member property:

```js
Resource.extend(Person, {
  static: {
    uri: '/api/v1/people'
  }
});
```

Mixins or multiple inhertiance allows for adding methods from multiple classes.

```js
// Person will inherit from Resource, but will also get the methods from EventTarget
function Person(data) {
  // Calling both constructors to ensure we are initialized correctly
  Resource.call(this, data);
  EventTarget.call(this);
}

Resource.extend(Person, EventTarget, {
  save: function() {
    this.dispatchEvent(new Event('save'));
    return Resource.prototype.save.call(this);
  }
});

var p = new Person();
p.on('save', function() {
  console.log('Person was saved!');
});
```

The extended mixins and the prototype of the class can safely use getters/setters.

```js
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

Class.extend(Person, {
  get name() {
    return this.firstName + ' ' + this.lastName;
  },
  set name(value) {
    var parts = value.split(' ');
    this.firstName = parts[0];
    this.lastName = parts[1];
  }
});

function Friend(firstName, lastName, nickName) {
  Person.call(this, firstName, lastName);
  this.nickName = nickName;
}

Person.extend(Friend, {
  get name() {
    return this.nickName;
  },
  set name(value) {
    this.nickName = value;
  }
});

var rando = new Person('John', 'Smith');
var friend = new Friend('Robert', 'Haroldson', 'Bob');
console.log(rando.name); // John Smith
console.log(friend.name); // Bob
friend.name = 'Rob';
console.log(friend.nickName); // Rob
```

## EventTarget

This utility ties into the browser eventing system and exposes it for your libraries to use. This does not work in
node.js. `EventTarget` extends from `Class` so anything extending it will take advantage of the extension system. Or it
may be used as a mixin as described above, just be sure to call the constructor within your subclass' constructor.

To extend the event target:

```js
var EventTarget = require('chip-utils/event-target');

function MyClass() {
  EventTarget.call(this);
}

EventTarget.extend(MyClass, {
  save: function() {
    // Use the cancelable flag to allow actions to be canceled by listeners
    var event = new Event('saving', { cancelable: true });
    this.dispatchEvent(event);

    if (!event.defaultPrevented) {
      var self = this;

      $.ajax(
        type: 'PUT',
        url: '/items/' + this.id,
        contentType: 'application/json',
        data: JSON.stringify(this)
      ).then(function() {
        // Signal the save is complete
        self.dispatchEvent(new Event('save'));
      }, function(err) {
        // Use custom events to add additional data
        self.dispatchEvent(new CustomEvent('error', { detail: err}));
      });
    }
  }
});
```

To listen to events on an event target (or remove listeners:

```js
var obj = new MyClass();

obj.on('saving', function(event) {
  if (event.target.ownerId !== me.id) {
    event.preventDefault();
  }
});

// Will only get called once
obj.one('save', function() {
  alert('First save');
});

// `on` and `off` are aliases of `addEventListener` and `removeEventListener`
obj.addEventListener('save', myListener);
obj.removeEventListener('save', myListener);
obj.on('save', myListener);
obj.off('save', myListener);
```

### Note

This EventTarget class will only work in the browser (or jsdom) since it uses a native EventTarget to work with the
browser's eventing system. It does not support bubbling.

---
_Source: https://npm.io/package/chip-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
