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

synonomous

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

synonomous - npm Package Compare versions

Comparing version 2.0.1 to 2.1.0

48

index.js

@@ -32,4 +32,5 @@ 'use strict';

/**
* @summary Default list of active registered transformations by name.
* @summary Default list of active registered transformations by _or_ an object whose keys name the transformations.
* @desc Used by {@link Synonomous.prototype.getSynonyms} and {@link Synonomous.prototype.decorateList}.
* When an object, the former just uses the keys, ignoring the values, while the latter uses both the keys and the values.
*

@@ -42,3 +43,3 @@ * An override may be defined on the instance, easily done by supplying as a constructor option.

* @default
* @type {string[]}
* @type {string[]|object}
* @memberOf Synonomous#

@@ -106,5 +107,5 @@ */

/**
* If `name` is a string and non-blank, returns an array containing unique non-blank converted names.
* If `name` is a string and non-blank, returns an array containing unique non-blank synonyms of `name` generated by the transformer functions named in `this.transformations`.
* @param {string} name - String to make synonyms of.
* @parma {string[]} transformations - When provided, temporarily overrides `this.transformations`.
* @parma {string[]|object} transformations - When provided, temporarily overrides `this.transformations`.
* @memberOf Synonomous#

@@ -115,3 +116,7 @@ */

if (typeof name === 'string' && name) {
(transformations || this.transformations).forEach(function(key) {
transformations = transformations || this.transformations;
if (!Array.isArray(transformations)) {
transformations = Object.keys(transformations);
}
transformations.forEach(function(key) {
if (typeof transformers[key] !== 'function') {

@@ -152,7 +157,13 @@ throw new ReferenceError('Unknown transformer "' + key + '"');

*
* That is, each element is either converted to a string or is an object with a property named `propPath` converted to a string.
* That is, each element is either iteself converted to a string; or is an object with a property named by following `propPath` which is converted to a string.
*
* All transformers named in `transformations` are run on that string and all the resulting unique non-blank "synonyms" are added as properties to the array with the value of the property being a reference to the element (if it was an object) or a copy of the element (if it was a string), subject to the following rules:
* 1. Duplicate synonyms are not added.
* 2. Blank synonyms are not added.
* For each element, all transformers named in `this.transformations` are run on that string.
* * _When `this.transformations` is an array:_
* **Create dictionary entries (synonyms) for the element.**
* Specifically: All the resulting unique non-blank "synonyms" are added as properties to the array with the value of the property being a reference to the element (if it was an object) or a copy of the element (if it was a string), subject to the following rules:
* 1. Duplicate synonyms are not added.
* 2. Blank synonyms are not added.
* * _When `this.transformations` is an non-array object:_
* **Create a new property inside the element for each transformation.**
* Specifically: The keys of `this.transformations` name the transformers. The values are dot-paths (dot-separated-strings or arrays) to properties inside each element, set to the string returned by the transformer named by the key.
*

@@ -179,4 +190,8 @@ * @param {number} [index] - Index of element of `list` to add synonyms for. If omitted:

var value = propPath !== undefined && typeof item === 'object' ? drilldown(item, propPath) : item;
var synonyms = this.getSynonyms(value);
this.decorate(list, synonyms, item);
if (Array.isArray(this.transformations)) {
var synonyms = this.getSynonyms(value);
this.decorate(list, synonyms, item);
} else {
Object.keys(this.transformations).forEach(injectTransformedValueIntoItem.bind(this, item, value));
}
}, this);

@@ -221,2 +236,13 @@ return list;

function injectTransformedValueIntoItem(item, value, transformation) {
var transformer = transformers[transformation],
path = this.transformations[transformation],
pathList = Array.isArray(path) ? path.slice() : path.split('.'),
propName = pathList.splice(pathList.length - 1, 1)[0],
drillDownContext = drilldown(item, pathList);
drillDownContext[propName] = transformer(value);
}
module.exports = Synonomous;
{
"name": "synonomous",
"version": "2.0.1",
"version": "2.1.0",
"description": "Decorate an array instance with synonyms of its elements",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -63,3 +63,3 @@ # synonomous

var propNames = ['width', 'thickness'];
decorator.decorateObject(myObject, propNames, value);
decorator.decorateObject(myObject, propNames, value); // aka `decorate`
```

@@ -115,9 +115,9 @@ Mutates and returns `myObject`:

`decorator.dictPath` can be customized in the same manner as described above ([_Setting a custom instance default_](#setting-a-custom-instance-default) for `decorator.transformations`.
`decorator.dictPath` can be customized in the same manner as described above in [_Setting a custom instance default_](#setting-a-custom-instance-default) for `decorator.transformations`.
### Decorate an array with synonyms
Here we arrive at the real utility of this `synonomous` module. To add synonyms as properties to an array of objects, use the `decorateArray` (aka `decorateList`) method:
```js
var decorator = new Synonymous(['verbatim', 'toAllCaps', 'toCamelCase']);
var list = ['borderLeft', 'background-color'];
decorator.decorateArray(list); // synonym: `decorateList`
decorator.transformations = ['verbatim', 'toAllCaps', 'toCamelCase'];
decorator.decorateArray(list); // aka `decorateList`
```

@@ -136,5 +136,5 @@ This call decorates and returns `list` which would then look like this:

```
**Specifically, the array now has properties that point to elements whose keys are transformations of each element. This lets the array double as a dictionary to its own elements.**
**Specifically, the array now has new properties whose keys are transformations of each element _and whose values are references to them._ This lets the array double as a dictionary to its own elements.**
Note that this is not an enumeration; the property values are not integer indexes but rather references to the elements themselves.
Note that this is not an enumeration; the property values are not integer indexes but rather references to the elements themselves. Also note that when elements contain primitive values, the new synonyms properties' values will be copies of them rather than references to them.

@@ -144,4 +144,4 @@ #### Decorating with synonyms of a single element

```js
var decorator = new Synonymous;
var list = ['borderLeft', 'background-color'];
delete decorator.transformations; // revert to default (verbatim and camelCase)
decorator.decorateArray(1, list); // just decorate with synonyms for 2nd element

@@ -160,5 +160,4 @@ ```

#### Decorating with a property of an element
When elements are objects rather than string primitives, you can specify which property of such objects to make synonyms of with the overload `decorateArray(list: (string|object)[], propName: (string|array) = 'name')`:
When elements are objects rather than string primitives, you can specify which property of such objects to make synonyms of with the overload `decorator.decorateArray(list: (string|object)[], propPath: (string|array) = decorator.propPath)`:
```js
var decorator = new Synonymous;
var list = [

@@ -181,8 +180,8 @@ { style: 'borderLeft', value: '8px' },

Notes:
1. The `propName` parameter defaults to the value of the `decorator.propName` property when omitted. When so defined, _and_ the list element is an object, `propName` is used to drill down into the list element. Otherwise the list element iteself (coerced to a string) is used as the source for the synonyms.
2. Both the `propName` parameter and the `decorator.propName` property can be a dot-path (or an array), similar to `decorator.dictPath`.
3. `decorator.propName` can be customized in the same manner as described above ([_Setting a custom instance default_](#setting-a-custom-instance-default) for `decorator.dictPath` and `decorator.transformations`.
1. When omitted, the `propPath` parameter defaults to the value of the `decorator.propPath` property, whose initial value is `'name'`. When the list element is an object, this path is used to drill down into the list element. Otherwise (if path is undefined, even after applying the default _or_ the list element is not an object), the list element iteself (coerced to a string) is used as the source for the synonyms.
2. Both the `propPath` parameter and the `decorator.propPath` property can be a dot-path (or an array), similar to `decorator.dictPath`.
3. `decorator.propPath` can be customized in the same manner as described above in [_Setting a custom instance default_](#setting-a-custom-instance-default) for `decorator.dictPath` and `decorator.transformations`.
#### Decorating with synonyms of a property of a single element
You can of course combine these two features with the overload `decorateArray(index: number, list: object[], propName: (string|array) = 'name')`:
You can of course combine these two features with the overload `decorator.decorateArray(index: number, list: object[], propPath: (string|array) = decorator.propPath)`:
```js

@@ -193,2 +192,37 @@ decorator.decorateArray(1, list, 'style');

#### Decorating elements with strings
As an alterative to decorating the list with synonyms of the elements, you can also decorate the elements themselves with the synonym strings. To do this, set `this.transformations` to an object rather than an array of strings. The keys are the transformer names and the values are used as dot-paths into each element to put the string result of the transformer call.
```js
this.transformers = {
toCamelCase: 'id',
toTitle: 'info.title'
};
var list = [
{ style: 'borderLeft', value: '8px' },
{ style: 'background-color', value: 'pink' }
];
decorator.decorateArray(list, 'style');
```
list` now looks like this:
```js
{
0: {
style: 'borderLeft',
value: '8px',
id: 'borderLeft',
info: {
title: 'Border Left'
}
},
1: {
style: 'background-color',
value: 'pink' ,
id: 'backgroundColor',
info: {
title: 'Background Color'
}
}
```
### Using synonyms in code

@@ -200,5 +234,16 @@

### Instantiation options
The constructor takes a single optional parameter, an `options` object, which is a shorthand method of setting the `transformations`, `dictPath`, and `propPath` properties.
For example, to set up a `header` field for each element, based on its `name` field`:
```js
var decorator = new Decorator({
transformations: ['toTitle'],
propPath: 'name',
dictPath: '*.header'
}
### Revision History
* **2.0.1** —
* **2.1.0** — `transformations` property can now be an object as well an array of strings. The keys of the object name the transformers. The values are not used by `decorateObject` but are used by `decorateArray` to add new string properties to each element.
* **2.0.1**
* Fixed `decorate` to return the given object itself (instead of the drill-down context within the given object).

@@ -210,3 +255,3 @@ * Added two method a.k.a.'s to clarify the intent of each:

* Added `decorate` method and `dictPath` property.
* Changed `propName` property to `propPath`. _(Breaking change if property used.)_
* Changed `propPath` property to `propPath`. _(Breaking change if property used.)_
* Changed optional constructor params to a single `options` object. _(Breaking change if params were used.)_

@@ -213,0 +258,0 @@ * Moved transformers to their own file. _(Breaking change if you want to override and exsiting transformer or add a custom transformer.)_

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