npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ultimate-descriptor

v2.0.4

Published

Powerful tool to work with descriptors

Downloads

12

Readme

UltimateDescriptor

Adds cool features to working with descriptor
Allows to create object connections of high complexity using generators

Usage:

const Descriptor = require( 'ultimate-descriptor' );

/* ------------ Tools ------------- */

// returns new Descriptor object
// Descriptor( {descriptor:Object|Descriptor}[, {originObj:Object}, {originProp:String}] ) => {Descriptor}

// extracts descriptor form object
// Descriptor.get( {obj:Object}, {prop:String}[, {returnDescriptor:Boolean}] ) => {Object|Descriptor|undefined}

// extends descriptor with others
// Descriptor.extend( {descriptor1:Object|Descriptor}[, ...] ) => {Object|Descriptor}

// converts object with property descriptors to result object
// Descriptor.toObject( {propDescriptors:Object} ) => {Object}

// Returns object clone where all properties are defined via origin object's descriptors
// Descriptor.clone( {obj:Object} ) => {Object}

// returns generator constructor
// Descriptor.generator( {generatorFunc:Function} ) => {Generator}

// Returns new object with replaced descriptors
// Descriptor.replace( [{deep:Boolean},] {obj:Object}, {descriptors:Array|Generator|Object} )
// to set filtering on which descriptor to use try this:
// descriptors = [ { filter: function ( prop, obj ) { return prop == 'test' }, descriptor: YourDescr } ]


/* ------------ Example vars ------------- */

var obj1 = { _phrase: 'Hello' },
    obj2 = { _phrase: 'Goodbye' };

Object.defineProperties( obj1, {
  say: { value: function () { return this.phrase } },

  phrase: {
    get: function () { return this._phrase },
    set: function ( value ) { this._phrase = value },
    configurable: true
  }
});


/* ------------ Simple usage ------------- */

var descriptor = Descriptor.get( obj1, 'phrase' );

console.log( obj2.phrase );           // undefined

descriptor.assignTo( obj2, 'phrase' );

console.log( obj2.phrase );           // Goodbye

descriptor
  .extend( { get: function () { return this._phrase + ' world!' } } )
  .extend( Descriptor( { set: function ( value ) { this._phrase = value + ' mighty' } } ) )
  .assignTo( obj2, 'phrase' );

console.log( obj2.phrase );           // Goodbye world!

obj2.phrase = 'Hello';

console.log( obj2.phrase );           // Hello mighty world!

// var result = Descriptor.extend( descriptor1, descriptor2 ); // result === descriptor1


/* ------------ Advanced usage ------------- */

// Descriptor.generator is used to generate final descriptor value using some function and originObj
// Descriptor.generator can be used on whole descriptor or on any property

var valueSayDescriptor = {
    value: Descriptor.generator( function ( originDescr, originObj, originProp, objProp ) {
      return function () { return originDescr.value.apply( originObj, arguments ) + ' world!' }
    }).for( 'say' ), // defining descriptor generator using property 'say' of some object
    configurable: true
  },
  getSetDescriptor = Descriptor.generator( {
    get: function ( originDescr, originObj, originProp, objProp ) {
      return function () { return originDescr.get.call( originObj ) + ' world!' }
    },
    set: function ( originDescr, originObj, originProp, objProp ) {
      return function ( value ) { originDescr.set.call( originObj, value + ' mighty' ) }
    },
    configurable: function ( originDescr, originObj, originProp, objProp ) {
      var originConfigurable = originDescr.configurable;
      return originConfigurable !== undefined ? !originConfigurable : true
    }
  });

// e.g. we want to connect obj2.whisper() with obj1.say() using generator
var descriptor = Descriptor( valueSayDescriptor, obj1 );

descriptor.assignTo( obj2, 'whisper' );

console.log( obj2.whisper() );          // Hello world!

// now lets use obj1.phrase in generator using Proxy
// with proxy changing obj2 will change obj1 and vice versa
// they become connected via descriptor generators
var descriptor = Descriptor( getSetDescriptor ).for( obj1, 'phrase' );

descriptor.asProxy().assignTo( obj2, 'phrase' );

console.log( obj1.phrase );           // Hello

obj2.phrase = 'Hi';

console.log( obj1.phrase );           // Hi mighty
console.log( obj2.phrase );           // Hi mighty world!


var obj3 = {};

// asProxy() here is not necessary it is still set to true from previous time
// to disable proxy use asProxy( false )
descriptor.for( obj2, 'phrase' ).asProxy().assignTo( obj3, 'phrase' );

console.log( obj1.phrase );           // Hi mighty
console.log( obj2.phrase );           // Hi mighty world!
console.log( obj3.phrase );           // Hi mighty world! world!

// now we can see how all these objects are connected
obj3.phrase = 'Oh no';

console.log( obj1.phrase );           // Oh no mighty mighty
console.log( obj2.phrase );           // Oh no mighty mighty world!
console.log( obj3.phrase );           // Oh no mighty mighty world! world!