Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

minim

Package Overview
Dependencies
Maintainers
2
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minim - npm Package Compare versions

Comparing version 0.10.0 to 0.11.0

lib/namespace.js

31

CHANGELOG.md

@@ -0,1 +1,32 @@

# 0.11.0 - 2015-09-07
- **BREAKING** The public interface of the `minim` module has changed significantly. List of changes:
- `ElementRegistry` has been renamed to `Namespace`.
- `minim` has only one public method, called `namespace`, which creates a new `Namespace` instance.
- `minim.convertToElement` is now `namespace.toElement`
- `minim.convertFromRefract` is now `namespace.fromRefract`
- `minim.convertFromCompactRefract` is now `namespace.fromCompactRefract`
- `minim.*Element` are removed (except for `namespace.BaseElement`). These should be accessed via `namespace.getElementClass('name')` now.
- The `Namespace` has a new method `use` which loads a plugin namespace and is chainable, e.g. `namespace.use(plugin1).use(plugin2)`.
- A `Namespace` can be initialized without any default elements by passing an options object with `noDefault` set to `false` to the constructor. They can be initialized later via the `useDefault` method.
Before:
```js
var minim = require('minim');
minim.convertToElement([1, 2, 3]);
```
After:
```js
var minim = require('minim');
var namespace = minim.namespace();
namespace.toElement([1, 2, 3]);
```
- Add a `.toValue()` method to member elements which returns a hash with the key
and value and their respective values.
# 0.10.0 - 2015-08-18

@@ -2,0 +33,0 @@

42

lib/minim.js
'use strict';
var _ = require('lodash');
var base = require('./base');
var registry = base.registry;
var Namespace = require('./namespace');
// Set up classes for default elements
registry
.register('null', base.NullElement)
.register('string', base.StringElement)
.register('number', base.NumberElement)
.register('boolean', base.BooleanElement)
.register('array', base.ArrayElement)
.register('object', base.ObjectElement)
.register('member', base.MemberElement);
// Direct access to the Namespace class
exports.Namespace = Namespace;
// Add instance detection functions to convert existing objects into
// the corresponding refract elements.
registry
.detect(_.isNull, base.NullElement, false)
.detect(_.isString, base.StringElement, false)
.detect(_.isNumber, base.NumberElement, false)
.detect(_.isBoolean, base.BooleanElement, false)
.detect(_.isArray, base.ArrayElement, false)
.detect(_.isObject, base.ObjectElement, false);
module.exports = _.extend({
convertToElement: function() {
return registry.toElement.apply(registry, arguments);
},
convertFromRefract: function() {
return registry.fromRefract.apply(registry, arguments);
},
convertFromCompactRefract: function() {
return registry.fromCompactRefract.apply(registry, arguments);
}
},
base
);
// Special constructor for the Namespace class
exports.namespace = function namespace(options) {
return new Namespace(options);
};

@@ -15,2 +15,9 @@ 'use strict';

toValue: function () {
return {
key: this.key.toValue(),
value: this.value.toValue()
};
},
toRefract: function() {

@@ -17,0 +24,0 @@ return {

{
"name": "minim",
"version": "0.10.0",
"version": "0.11.0",
"description": "A library for interacting with JSON through Refract elements",

@@ -5,0 +5,0 @@ "main": "lib/minim.js",

@@ -42,4 +42,4 @@ # Minim

```javascript
var minim = require('minim');
var arrayElement = minim.convertToElement([1, 2, 3]);
var minim = require('minim').namespace();
var arrayElement = minim.toElement([1, 2, 3]);
var refract = arrayElement.toRefract();

@@ -83,5 +83,5 @@ ```

```javascript
var arrayElement1 = minim.convertToElement([1, 2, 3]);
var arrayElement1 = minim.toElement([1, 2, 3]);
var refracted = arrayElement1.toRefract();
var arrayElement2 = minim.convertFromRefract(refracted);
var arrayElement2 = minim.fromRefract(refracted);
```

@@ -94,2 +94,3 @@

```javascript
var StringElement = minim.getElementClass('string');
var NewElement = StringElement.extend({

@@ -136,3 +137,3 @@ constructor: function() {

```javascript
var arrayElement = minim.convertToElement([1, 2, 3]);
var arrayElement = minim.toElement([1, 2, 3]);
var arrayValue = arrayElement.toValue(); // [1, 2, 3]

@@ -146,3 +147,3 @@ ```

```javascript
var arrayElement = minim.convertToElement([1, 2, 3]);
var arrayElement = minim.toElement([1, 2, 3]);
var refract = arrayElement.toRefract(); // See converting to elements above

@@ -156,3 +157,3 @@ ```

```javascript
var stringElement = minim.convertToElement("foobar");
var stringElement = minim.toElement("foobar");
var compact = stringElement.toCompactRefract(); // ['string', {}, {}, 'foobar']

@@ -166,3 +167,3 @@ ```

```javascript
var stringElement = minim.convertToElement("foobar");
var stringElement = minim.toElement("foobar");
stringElement.equals('abcd'); // returns false

@@ -176,3 +177,3 @@ ```

```javascript
var stringElement = minim.convertToElement("foobar");
var stringElement = minim.toElement("foobar");
var stringElementClone = stringElement.clone();

@@ -198,3 +199,3 @@ ```

```javascript
var stringElement = new minim.StringElement();
var stringElement = minim.toElement('');
stringElement.set('foobar');

@@ -213,3 +214,3 @@ var value = stringElement.get() // get() returns 'foobar'

```javascript
var numberElement = new minim.NumberElement();
var numberElement = minim.toElement(0);
numberElement.set(4);

@@ -228,5 +229,5 @@ var value = numberElement.get() // get() returns 4

```javascript
var booleanElement = new minim.BooleanElement();
var booleanElement = minim.toElement(false);
booleanElement.set(true);
var value = booleanElement.get() // get() returns 4
var value = booleanElement.get() // get() returns true
```

@@ -243,3 +244,3 @@

```js
const arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
const arrayElement = minim.toElement(['a', 'b', 'c']);

@@ -256,3 +257,3 @@ for (let item of arrayElement) {

```javascript
var arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
var arrayElement = minim.toElement(['a', 'b', 'c']);
var value = arrayElement.get(0) // get(0) returns item for 'a'

@@ -266,3 +267,3 @@ ```

```javascript
var arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
var arrayElement = minim.toElement(['a', 'b', 'c']);
var value = arrayElement.getValue(0) // get(0) returns 'a'

@@ -276,3 +277,3 @@ ```

```javascript
var arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
var arrayElement = minim.toElement(['a', 'b', 'c']);
var value = arrayElement.getIndex(0) // returns the item for 'a'

@@ -286,3 +287,3 @@ ```

```javascript
var arrayElement = new minim.ArrayElement();
var arrayElement = minim.toElement([]);
arrayElement.set(0, 'z');

@@ -297,3 +298,3 @@ var value = arrayElement.get(0) // get(0) returns 'z'

```javascript
var arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
var arrayElement =minim.toElement(['a', 'b', 'c']);
var newArray = arrayElement.map(function(item) {

@@ -309,3 +310,3 @@ return item.element;

```javascript
var arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
var arrayElement = minim.toElement(['a', 'b', 'c']);
var newArray = arrayElement.filter(function(item) {

@@ -321,3 +322,3 @@ return item.get() === 'a'

```javascript
var numbers = new minim.ArrayElement([1, 2, 3, 4]);
var numbers = minim.toElement([1, 2, 3, 4]);
var total = numbers.reduce(function(result, item) {

@@ -331,3 +332,3 @@ return result.toValue() + item.toValue();

```javascript
var numbers = new minim.ArrayElement([1, 2, 3, 4]);
var numbers = minim.toElement([1, 2, 3, 4]);
var total = numbers.reduce(function(result, item) {

@@ -341,3 +342,3 @@ return result.toValue() + item.toValue();

```javascript
var objNumbers = new minim.ObjectElement({a: 1, b:2, c:3, d:4});
var objNumbers = minim.toElement({a: 1, b:2, c:3, d:4});
var total = objNumbers.reduce(function(result, item) {

@@ -368,3 +369,3 @@ return result.toValue() + item.toValue();

```javascript
var arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
var arrayElement = minim.toElement(['a', 'b', 'c']);
arrayElement.forEach(function(item) {

@@ -380,3 +381,3 @@ console.log(item.toValue())

```javascript
var arrayElement = new minim.ArrayElement(['a', 'b', 'c']);
var arrayElement = minim.toElement(['a', 'b', 'c']);
arrayElement.push('d');

@@ -391,3 +392,3 @@ console.log(arrayElement.toValue()); // ['a', 'b', 'c', 'd']

```javascript
var arrayElement = new minim.ArrayElement(['a', [1, 2], 'b', 3]);
var arrayElement = minim.toElement(['a', [1, 2], 'b', 3]);
var numbers = arrayElement.find(function(el) {

@@ -411,3 +412,3 @@ return el.element === 'number'

```javascript
var arrayElement = new minim.ArrayElement(['a', [1, 2], 'b', 3]);
var arrayElement = minim.toElement(['a', [1, 2], 'b', 3]);
var numbers = arrayElement.children(function(el) {

@@ -433,3 +434,3 @@ return el.element === 'number';

```javascript
var arrayElement = new minim.ArrayElement(['a', [1, 2], 'b', 3]);
var arrayElement = minim.toElement(['a', [1, 2], 'b', 3]);
arrayElement.contains('a'); // returns true

@@ -443,3 +444,3 @@ ```

```javascript
var arrayElement = new minim.ArrayElement(['a', [1, 2], 'b', 3]);
var arrayElement = minim.toElement(['a', [1, 2], 'b', 3]);
arrayElement.first(); // returns the element for "a"

@@ -453,3 +454,3 @@ ```

```javascript
var arrayElement = new minim.ArrayElement(['a', [1, 2], 'b', 3]);
var arrayElement = minim.toElement(['a', [1, 2], 'b', 3]);
arrayElement.second(); // returns the element for "[1, 2]"

@@ -463,3 +464,3 @@ ```

```javascript
var arrayElement = new minim.ArrayElement(['a', [1, 2], 'b', 3]);
var arrayElement = minim.toElement(['a', [1, 2], 'b', 3]);
arrayElement.last(); // returns the element for "3"

@@ -478,3 +479,3 @@ ```

```javascript
var objectElement = new minim.ObjectElement({ foo: 'bar' });
var objectElement = minim.toElement({ foo: 'bar' });
var value = objectElement.get('foo') // returns string instance for 'bar'

@@ -488,3 +489,3 @@ ```

```javascript
var objectElement = new minim.ObjectElement({ foo: 'bar' });
var objectElement = minim.toElement({ foo: 'bar' });
var value = objectElement.getValue('foo') // returns 'bar'

@@ -498,3 +499,3 @@ ```

```javascript
var objectElement = new minim.ObjectElement({ foo: 'bar' });
var objectElement = minim.toElement({ foo: 'bar' });
var key = objectElement.getKey('foo') // returns the key element instance

@@ -508,3 +509,3 @@ ```

```javascript
var objectElement = new minim.ObjectElement({ foo: 'bar' });
var objectElement = minim.toElement({ foo: 'bar' });
var member = objectElement.getMember('foo') // returns the member element

@@ -520,3 +521,3 @@ var key = member.key; // returns what getKey('foo') returns

```javascript
var objectElement = new minim.ObjectElement();
var objectElement = minim.toElement({});
objectElement.set('foo', 'hello world');

@@ -531,3 +532,3 @@ var value = objectElement.get('foo') // get('foo') returns 'hello world'

```javascript
var objectElement = new minim.ObjectElement({ foo: 'bar' });
var objectElement = minim.toElement({ foo: 'bar' });
var keys = objectElement.keys() // ['foo']

@@ -541,3 +542,3 @@ ```

```javascript
var objectElement = new minim.ObjectElement({ foo: 'bar' });
var objectElement = minim.toElement({ foo: 'bar' });
var values = objectElement.values() // ['bar']

@@ -551,3 +552,3 @@ ```

```js
const objectElement = new minim.ObjectElement({ foo: 'bar' });
const objectElement = minim.toElement({ foo: 'bar' });

@@ -566,3 +567,3 @@ for (let [key, value] of objectElement.items()) {

```js
const objectElement = new minim.ObjectElement({ foo: 'bar' });
const objectElement = minim.toElement({ foo: 'bar' });
const values = objectElement.map((value, key, member) => {

@@ -576,3 +577,3 @@ // key is an instance for foo

### Element Registry
### Customizing Namespaces

@@ -582,6 +583,7 @@ Minim allows you to register custom elements. For example, if the element name you wish to handle is called `category` and it should be handled like an array:

```javascript
var minim = require('minim');
var minim = require('minim').namespace();
var ArrayElement = minim.getElementClass('array');
// Register your custom element
minim.registry.register('category', minim.ArrayElement);
minim.register('category', ArrayElement);

@@ -596,5 +598,28 @@ // Load serialized refract elements that includes the new element

// Unregister your custom element
minim.registry.unregister('category');
minim.unregister('category');
```
#### Creating Namespace Plugins
It is also possible to create plugin modules that define elements for custom namespaces. Plugin modules should export a single `namespace` function that takes an `options` object which contains an existing namespace to which you can add your elements:
```javascript
var minim = require('minim').namespace();
// Define your plugin module (normally done in a separate file)
var plugin = {
namespace: function(options) {
var base = options.base;
var ArrayElement = base.getElementClass('array');
base.register('category', ArrayElement);
return base;
}
}
// Load the plugin
minim.use(plugin);
```
### Chaining

@@ -605,3 +630,3 @@

```javascript
var objectElement = new minim.ObjectElement()
var objectElement = minim.toElement({})
.set('name', 'John Doe')

@@ -608,0 +633,0 @@ .set('email', 'john@example.com')

var _ = require('lodash');
var expect = require('./spec-helper').expect;
var minim = require('../lib/minim');
var minim = require('../lib/minim').namespace();

@@ -12,3 +12,3 @@ describe('Minim Converters', function() {

before(function() {
returnedElement = minim.convertToElement(val);
returnedElement = minim.toElement(val);
});

@@ -38,3 +38,3 @@

before(function() {
returnedElement = minim.convertFromRefract(el);
returnedElement = minim.fromRefract(el);
});

@@ -57,3 +57,3 @@

// does NOT handle nested long-form elements.
returnedElement = minim.convertFromCompactRefract([
returnedElement = minim.fromCompactRefract([
el.element, el.metadata, el.attributes, el.content

@@ -109,3 +109,3 @@ ]);

before(function() {
returnedElement = minim.convertFromRefract(el);
returnedElement = minim.fromRefract(el);
});

@@ -169,3 +169,3 @@

before(function() {
returnedElement = minim.convertFromRefract(el);
returnedElement = minim.fromRefract(el);
});

@@ -172,0 +172,0 @@

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();
var ArrayElement = minim.getElementClass('array');
describe('ArrayElement', function() {

@@ -10,3 +12,3 @@ context('value methods', function() {

function setArray() {
arrayElement = new minim.ArrayElement(['a', true, null, 1]);
arrayElement = new ArrayElement(['a', true, null, 1]);
}

@@ -128,3 +130,3 @@

describe('#getIndex', function() {
var numbers = new minim.ArrayElement([1, 2, 3, 4]);
var numbers = new ArrayElement([1, 2, 3, 4]);

@@ -163,3 +165,3 @@ it('returns the correct item', function() {

describe('#reduce', function() {
var numbers = new minim.ArrayElement([1, 2, 3, 4]);
var numbers = new ArrayElement([1, 2, 3, 4]);

@@ -240,3 +242,3 @@ it('sends index and array elements', function () {

var clone = arrayElement.clone();
expect(clone).to.be.instanceOf(minim.ArrayElement);
expect(clone).to.be.instanceOf(ArrayElement);
expect(clone).to.not.equal(arrayElement);

@@ -319,3 +321,3 @@ expect(clone.toRefract()).to.deep.equal(arrayElement.toRefract());

before(function() {
doc = minim.convertFromRefract(refract);
doc = minim.fromRefract(refract);
strings = doc.children(function(el) {

@@ -322,0 +324,0 @@ return el.element === 'string';

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();

@@ -5,0 +5,0 @@ describe('BaseElement', function() {

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();
var BooleanElement = minim.getElementClass('boolean');
describe('BooleanElement', function() {

@@ -9,3 +11,3 @@ var booleanElement;

before(function() {
booleanElement = new minim.BooleanElement(true);
booleanElement = new BooleanElement(true);
});

@@ -68,3 +70,3 @@

var clone = booleanElement.clone();
expect(clone).to.be.instanceOf(minim.BooleanElement);
expect(clone).to.be.instanceOf(BooleanElement);
expect(clone).to.not.equal(booleanElement);

@@ -71,0 +73,0 @@ expect(clone.toRefract()).to.deep.equal(booleanElement.toRefract());

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();
var MemberElement = minim.getElementClass('member');
describe('MemberElement', function() {
var member = new minim.MemberElement('foo', 'bar', {}, { foo: 'bar' });
var member = new MemberElement('foo', 'bar', {}, { foo: 'bar' });

@@ -17,2 +19,8 @@ it('correctly sets the key and value', function() {

describe('#toValue', function () {
it('returns a hash with key and value', function () {
expect(member.toValue()).to.deep.equal({ key: 'foo', value: 'bar' });
});
});
describe('#toRefract', function() {

@@ -19,0 +27,0 @@ it('returns the correct Refract value', function() {

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();
var NullElement = minim.getElementClass('null');
describe('NullElement', function() {

@@ -9,3 +11,3 @@ var nullElement;

before(function() {
nullElement = new minim.NullElement();
nullElement = new NullElement();
});

@@ -66,3 +68,3 @@

var clone = nullElement.clone();
expect(clone).to.be.instanceOf(minim.NullElement);
expect(clone).to.be.instanceOf(NullElement);
expect(clone).to.not.equal(nullElement);

@@ -69,0 +71,0 @@ expect(clone.toRefract()).to.deep.equal(nullElement.toRefract());

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();
var NumberElement = minim.getElementClass('number');
describe('NumberElement', function() {

@@ -9,3 +11,3 @@ var numberElement;

before(function() {
numberElement = new minim.NumberElement(4);
numberElement = new NumberElement(4);
});

@@ -68,3 +70,3 @@

var clone = numberElement.clone();
expect(clone).to.be.instanceOf(minim.NumberElement);
expect(clone).to.be.instanceOf(NumberElement);
expect(clone).to.not.equal(numberElement);

@@ -71,0 +73,0 @@ expect(clone.toRefract()).to.deep.equal(numberElement.toRefract());

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();
var ObjectElement = minim.getElementClass('object');
describe('ObjectElement', function() {

@@ -9,3 +11,3 @@ var objectElement;

function setObject() {
objectElement = new minim.ObjectElement({
objectElement = new ObjectElement({
foo: 'bar',

@@ -207,3 +209,3 @@ z: 1

it('accepts an object', function() {
var obj = new minim.ObjectElement();
var obj = new ObjectElement();
obj.set({ foo: 'bar' });

@@ -312,3 +314,3 @@ expect(obj.get('foo').toValue()).to.equal('bar');

describe('#reduce', function() {
var numbers = new minim.ObjectElement({
var numbers = new ObjectElement({
a: 1,

@@ -398,3 +400,3 @@ b: 2,

var clone = objectElement.clone();
expect(clone).to.be.instanceOf(minim.ObjectElement);
expect(clone).to.be.instanceOf(ObjectElement);
expect(clone).to.not.equal(objectElement);

@@ -401,0 +403,0 @@ expect(clone.toRefract()).to.deep.equal(objectElement.toRefract());

var _ = require('lodash');
var expect = require('../spec-helper').expect;
var minim = require('../../lib/minim');
var minim = require('../../lib/minim').namespace();
var StringElement = minim.getElementClass('string');
describe('StringElement', function() {

@@ -9,3 +11,3 @@ var stringElement;

before(function() {
stringElement = new minim.StringElement('foobar');
stringElement = new StringElement('foobar');
});

@@ -68,3 +70,3 @@

var clone = stringElement.clone();
expect(clone).to.be.instanceOf(minim.StringElement);
expect(clone).to.be.instanceOf(StringElement);
expect(clone).to.not.equal(stringElement);

@@ -71,0 +73,0 @@ expect(clone.toRefract()).to.deep.equal(stringElement.toRefract());

var _ = require('lodash');
var expect = require('./spec-helper').expect;
var minim = require('../lib/minim');
var minim = require('../lib/minim').namespace();
var ArrayElement = minim.getElementClass('array');
var StringElement = minim.getElementClass('string');
describe('Minim subclasses', function() {
// TODO: Provide better interface for extending elements
var MyElement = function() {
minim.StringElement.apply(this, arguments);
StringElement.apply(this, arguments);
this.element = 'myElement';

@@ -13,3 +16,3 @@ this._attributeElementKeys = ['headers'];

MyElement.prototype = _.create(minim.StringElement.prototype, {
MyElement.prototype = _.create(StringElement.prototype, {
ownMethod: function() {

@@ -58,3 +61,3 @@ return 'It works!';

it('should create headers element instance', function() {
expect(myElement.attributes.get('headers')).to.be.instanceof(minim.ArrayElement);
expect(myElement.attributes.get('headers')).to.be.instanceof(ArrayElement);
});

@@ -69,3 +72,3 @@

var myElement = new MyElement();
myElement.attributes.set('headers', new minim.ArrayElement(['application/json']));
myElement.attributes.set('headers', new ArrayElement(['application/json']));
myElement.attributes.get('headers').content[0].meta.set('name', 'Content-Type');

@@ -72,0 +75,0 @@

Sorry, the diff of this file is not supported yet

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