New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bau

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bau

A DSL to define object's in a fluent way.

  • 1.0.1
  • unpublished
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

bau

A JavaScript DSL to define objects in a fluent way.

npm install bau

Usage

var bau = require('bau');

.create()

Create a new instance to add modifiers.

var b = bau.create();

.define(name, fn)

Define a modifier fn on .prototype and make it chainable, i.e. it will return this in case fn returns nothing.

bau.define('foo', function (name, delta) {
  this.push(function (obj) {
    obj[name] += delta;
  });
});

var b = bau.create().foo('bar', 1);

var obj = b.apply({ bar: 2 });

obj.bar === 3;

.fork()

Create a separate DSL inheriting all existing extensions. Modifications to the new DSL will not apply to its ancestor.

var bau2 = bau.fork();

.use(fn)

Calls the given function with this as first argument. It is a shorthand for applying extensions.

bau.use(function (bau) {
  // Define extensions here.
});

.with

A shorthand for .create() to provide a more concise fluent DSL.

bau.with.someExtension();
// vs
bau.create().someExtension();

#apply(obj)

Apply all modifiers in the order they were added to a given object and return that object.

#create(proto)

Call #apply on a new object created from the given prototype or null if none is given.

#push(fn)

Add a modifier function.

b.push(function (obj) {
  obj.foo = 42;
});

var obj = b.apply({});

obj.foo === 42;

Bundled Modifiers

Bundled modifiers are only applied when importing via require('bau') which will use a fork of require('bau/lib/bau').

#accessor(...names)

Define ES5 getters and setters for the given names which are either given as multiple arguments or a single array. The getters and setters are defined as non-enumerable, non-configurable, and non-writable. If you only want getters or setters (but not both) use #getter or #setter respectively because getter and setter are defined via a single property which cannot be re-defined afterwards. They simply delegate to regular methods whose name is determined by capitalizing a name's first character (String#toUpperCase) and prefixing it with get or set respectively. The reason for this approach is that overriding a method and accessing the overridden method with a different context (Function#call or Function#apply) is easy. The same does not hold true for proper ES5 getters and setters which have to be accessed via a property descriptor's get and set properties in order to invoke Function#call or Function#apply to pass a different context to those functions.

b.accessor('foo');

var obj = b.create();

obj.foo;
// => obj.getFoo();

obj.foo = 42;
// => obj.setFoo(42);

#define(desc)

Define properties using Object.defineProperties.

b.define({
  someConstant: {
    value: 'bar'
  }
});

var obj = b.create();

obj.someConstant === 'bar';

#getter(...names)

Similar to #accessor but only defines getters.

b.getter('foo');

var obj = b.create();

obj.foo;
// => obj.getFoo();

#mixin(ext)

Mixin the properties of a some object obj using Object.defineProperty and the individual property descriptors.

b.mixin({
  someMethod: function () {
    console.log('yada yada yada');
  }
});

var obj = b.create();

obj.someMethod();
// => console.log('yada yada yada');

#override(ext)

Override properties with the properties and the result of their respective functions in the given object. Overridden properties are made accessible as first argument passed to the function returning the overriding value. This argument is the overridden value when overriding an object's own property. When overriding a property inherited via the prototype chain, the argument will be a function delegating to the current property accessible on the prototype.

b.override({
  hello: function (_super) {
    return function () {
      return _super.call(this) + ' Doe';
    };
  }
});

var obj = {
  name: 'Jane',
  hello: function () {
    return 'Hello ' + this.name;
  }
};

b.apply(obj);

obj.hello();
// => 'Hello Jane Doe';

#setter(...names)

Similar to #accessor but only defines setters.

b.setter('foo');

var obj = b.create();

obj.foo = 42;
// => obj.setFoo(42);

License

Licensed under the AGPL 3.0.

FAQs

Package last updated on 03 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc