1.1.2 • Published 7 years ago

lazyref v1.1.2

Weekly downloads
21
License
MIT
Repository
github
Last release
7 years ago

lazyref

Lazy, shape-shifting object references for metaprogramming

installation

npm install lazyref

usage

const { LazyObject, Symbols } = require( 'lazyref' )
const { equals, resolveAs }   = Symbols

const obj = new LazyObject

obj[ resolveAs ]( false )
console.log( obj instanceof Boolean )
// true

obj[ resolveAs ]( 2 )
console.log( obj instanceof Number, obj + 3 )
// true 5

obj[ resolveAs ]( 'world' )
console.log( obj instanceof String, obj.concat( ' world' ) )
// true "hello world"

obj[ resolveAs ]( [ 1, 2, 3 ] )
console.log( obj instanceof Array, obj.map( n => n ** 2 ) )
// true [ 1, 4, 9 ]

obj[ resolveAs ]( { works: 'yes' } )
console.log( obj instanceof Object, obj.works )
// true "yes"

obj[ resolveAs ]( what => what + ' is so meta' )
console.log( obj instanceof Function, obj.call( null, 'this' ) )
// true "this is so meta"

LazyObject exposes its own methods via Symbols to prevent clashing with the resolved object's own properties

specify a base

If you know the object type you will resolve to beforehand, you can also pass a base class to the constructor ( default is Object ) - this will also pass typeof checks

const o = new LazyObject( Function )
o[ resolveAs ]( hey => hey + ' there' )

console.log( typeof o, o( 'hey' ) )
// function "hey there"

equality check

const fs = require( 'fs' )

const o = new LazyObject
const q = new LazyObject

console.log( o[ equals ]( q ) )
//false

o[ resolveAs ]( fs )
console.log( o[ equals ]( fs ) )
// true

q[ resolveAs ]( fs )
console.log( o[ equals ]( q ) )
// true
1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago