1.3.21 • Published 9 months ago

attatch-to-prototype v1.3.21

Weekly downloads
51
License
ISC
Repository
github
Last release
9 months ago

Attach to prototype

Attach functions as non enumerable properties to the prototype of any object.

Please not that Attach to prototype is currently under development and not yet suited for production

Example

Generic

import { constructAttachToPrototype } from "attatch-to-prototype"

const attachToArray = constructAttachToPrototype(Array.prototype/*, options*/)
attachToArray("removeByValue", function(value) {
  const index = this.indexOf(value)
  if (index === -1) return
  this.splice(index, 1)
})

const ar = ["a", "b", "c"]

ar.removeByValue("b")

console.log(ar) // ["a", "c"]

Getter / Setter

Attach

attachToArray("last", {
  get() {
    return this[this.length-1]
  }
  set(to) {
    return this[this.length-1] = to
  }
})


let ar = [1, 2, 3]

ar.last      // return 3
ar.last = 0  // return 0

console.log(ar) // [1, 2, 0]

Apply

Just a different syntax to access getter / setter. Use Attach / apply depending on your coding conventions.

import { constructApplyToPrototype } from "attatch-to-prototype"

const attachToArray = constructApplyToPrototype(Array.prototype/*, options*/)
attachToArray("last", {
  get() {
    return this[this.length-1]
  }
  set(to) {
    return this[this.length-1] = to
  }
})


let ar = [1, 2, 3]

ar.last()   // return 3
ar.last(0)  // return 0

console.log(ar) // [1, 2, 0]

The generic functionality is also available on apply

Contribute

All feedback is appreciated. Create a pull request or write an issue.

1.3.21

9 months ago

1.3.20

11 months ago

1.3.18

12 months ago

1.3.19

12 months ago

1.3.17

2 years ago

1.3.13

2 years ago

1.3.14

2 years ago

1.3.12

2 years ago

1.3.15

2 years ago

1.3.16

2 years ago

1.3.11

3 years ago

1.3.10

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago