3.6.6 • Published 9 years ago

vigour-base v3.6.6

Weekly downloads
199
License
ISC
Repository
github
Last release
9 years ago

vigour-base

Build Status js-standard-style npm version Coverage Status

Extendable object constructors, build for speed, low memory consumption and simplicity

  • set method
  • easy extendable property defintions
  • deep, memory efficient prototypes
  • parent and key fields
  • types (easy inheritance)
  • inject pattern

###Properties The properties field is used to add property definitions for certain keys within set objects.

There are 4 types of property definitions:

  • true clears any special base behaviour for the key
  • function calls the function when the key is set instead of the normal behaviour
  • null removes property definition and any existing instances
  • anything else uses the set function

basic

var base = new Base({
  properties: {
    normal: true,
    special (val, stamp) {
      this.special = val * 10
    },
    base: { nested: true }
  }
})

base.set({
  normal: 'hello', // → 'hello'
  special: 10, // → 100
  base: 'a base' // → Base { val: 'a base', nested: true }
})

base.set({
  properties: {
    normal: null
    // removes property defintion and removes "normal"
  }
})

set

var special = new Base({
  type: 'special'
})

var base = new Base({
  properties: {
    // uses "noReference" for a base
    special: special
  }
})

base.set({
  special: 10 // → Special 10
})

// add something to the "special" property
base.set({
  properties: {
    special: {
      aField: true
    }
  }
})
// → base.special.aField.val === true, inherits from the property

define

Allows for extra customisation of property definitions.

It has 3 options:

  • :key when a property is set uses this key to store its value
  • :reset resets a previously defined property
  • :val property value
var base = new Base({
  properties: {
    define: {
      x: { key: 'y' },
      something: {
        key: 'else',
        val: {
          a: true,
          b: true
        }
      },
      hello: {
        key: 'bye',
        val: 100
      }
    }
  },
  x: 10 // → y: 10
  something: { c: true }, // → else: Base { a: true, b: true, c: true }
  hello: { field: true } // → bye: Base { val: 100, field: true }
})

base.set({
  properties: {
    define: {
      something: {
        // removes the "else" field on base
        // creates a new property definition for "something"
        reset: true,
        val: 'hello'
      },
      x: {
        key: 'z',
        reset: false // will not move "y"
      },
      hello: {
        // moves "bye" → "hello"
        key: null
      }
    }
  }
})

- ###Context Context enables deep memory efficient prototypes. Stores information on fields about first non-shared ancestors.

basic

Notice that base.a.b.c === instance.a.b.c is true but the paths are different

const base = new Base({
  key: 'base'
  a: { b: { c: 'its c' } }
})
const instance = new base.Constructor({
  key: 'instance'
})
console.log(base.a.b.c === instance.a.b.c) // → true
console.log(instance.a.b.c.path()) // → [ 'instance', 'a', 'b', 'c' ]
console.log(base.a.b.c.path()) // → [ 'base', 'a', 'b', 'c' ]

store and apply context

Allows storage and restoration of context. Usefull for edge cases where you need to make a handle to a nested field in a certain context

Consists of 2 methods

  • applyContext(context)
  • storeContext()
const base = new Base({
  key: 'base'
  a: { b: { c: 'its c' } }
})
const instance = new base.Constructor({
  key: 'instance'
})
const b = instance.a.b
const context = b.storeContext()
console.log(base.a.b.c) // this will remove the context "instance", and replace it with base
b.applyContext(context) // will reset the context of b to instance

Apply context can return 3 different types

  • undefined Context is restored without any differences
  • Base A set has happened in the path leading to the target of apply context
  • null A remove has happened in the path leading to the target of apply co ntext
3.6.6

9 years ago

3.6.5

9 years ago

3.6.4

9 years ago

3.6.3

9 years ago

3.6.2

9 years ago

3.6.1

9 years ago

3.6.0

9 years ago

3.5.2

9 years ago

3.5.1

9 years ago

3.5.0

9 years ago

3.4.2

9 years ago

3.4.1

9 years ago

3.4.0

9 years ago

3.3.14

9 years ago

3.3.13

9 years ago

3.3.12

9 years ago

3.3.11

9 years ago

3.3.10

9 years ago

3.3.9

9 years ago

3.3.8

9 years ago

3.3.7

9 years ago

3.3.6

9 years ago

3.3.5

9 years ago

3.3.4

9 years ago

3.3.3

9 years ago

3.3.2

9 years ago

3.3.1

9 years ago

3.3.0

9 years ago

3.2.0

9 years ago

3.1.1

9 years ago

3.1.0

9 years ago

3.0.0

9 years ago

2.3.2

9 years ago

2.3.1

9 years ago

2.3.0

9 years ago

2.2.11

9 years ago

2.2.10

9 years ago

2.2.9

9 years ago

2.2.8

9 years ago

2.2.7

9 years ago

2.2.6

9 years ago

2.2.5

9 years ago

2.2.4

9 years ago

2.2.3

9 years ago

2.2.2

9 years ago

2.2.1

9 years ago

2.2.0

9 years ago

2.1.10

9 years ago

2.1.9

9 years ago

2.1.8

9 years ago

2.1.7

9 years ago

2.1.6

9 years ago

2.1.5

9 years ago

2.1.4

9 years ago

2.1.3

9 years ago

2.1.2

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.2

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago