0.1.1 • Published 8 years ago

jscope v0.1.1

Weekly downloads
1
License
-
Repository
github
Last release
8 years ago

JScope

Build Status Coverage Status Climate Status Issues Open Issue Resolution

Version Node Downloads Slack Status License

JScope

NPM GRID

Motivation

Sometimes when you try write elegant code with fallback values you write something like this:

  // The Common case

  var foo = function(options){

      // Ensure option value to prevent undefined value
      options = options || {}; // !!WARNING This line do not force the value type, so if the object passed is not a plain object it will broke your code

      /*************************************/
      /* Default value fallback resolution */
      /*************************************/

      // Solved with ternary operator
      options.bar  = options.bar ? options.bar : 'foo';

      // Solved with if statement
      if(!options.bar)
        options.bar  = 'foo';

      // Solved with stream conditional operator
      options.bar  = options.bar || 'foo';

      return options;
  }

Let's do it easier!!

  // The JSCope case

  var jscope = require('jscope');

  var foo = function(options){
      options = jscope({
          'bar': 'foo',
          'foo': 'bar'
      }).$new(options);

      return options;
  }

  var result = foo({
      'frog': 'croac!',
      'bar': 'mooo'
  });

  // 'croac'
  console.log(result.frog);
  // 'bar'
  console.log(result.foo);
  // 'mooo'
  console.log(result.bar);
  // 'foo'
  console.log(result.$$root.bar);
  // 'foo'
  console.log(result.$$parent.bar);

  var child = result.$new({
      'bar': 'wow!!'
  });

  // 'foo'
  console.log(child.$$root.bar);
  // 'foo'
  console.log(child.$$parent.$$parent.bar);
  // 'mooo'
  console.log(child.$$parent.bar);
  // 'wow!!'
  console.log(child.bar);

As you can see you keep the data with persistence, and it's accesible by $$parent or $$root nodes, so your data never were so sure!!

Installation

Install with npm install jscope --save.

WTF

0.1.1

8 years ago

0.1.0

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.2

8 years ago