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.0 to 2.0.1

22

index.js

@@ -126,17 +126,17 @@ 'use strict';

/**
* Decorate a `collection` with properties named in `propNames` all referencing `item`.
* @param {object} collection - The object to decorate. If `this.dictPath` is defined, then decorate `collection[this.dictPath]` instead (created as needed).
* Decorate an object `obj` with properties named in `propNames` all referencing `item`.
* @param {object} obj - The object to decorate. If `this.dictPath` is defined, then decorate `obj[this.dictPath]` instead (created as needed).
*
* @param {string[]} propNames
* @param item
* @returns {object} `collection`
* @returns {object} `obj`, now with additional properties (possibly)
*/
decorate: function(collection, propNames, item) {
collection = drilldown(collection, this.dictPath);
decorate: function(obj, propNames, item) {
var drilldownContext = drilldown(obj, this.dictPath);
propNames.forEach(function(propName) {
if (!(propName in collection)) {
collection[propName] = item;
if (!(propName in drilldownContext)) {
drilldownContext[propName] = item;
}
});
return collection;
return obj;
},

@@ -182,2 +182,8 @@

// a.k.a.'s:
Synonomous.prototype.decorateObject = Synonomous.prototype.decorate;
Synonomous.prototype.decorateArray = Synonomous.prototype.decorateList;
function drilldown(collection, breadcrumbs) {

@@ -184,0 +190,0 @@ return breadcrumbs.reduce(function(result, crumb) {

{
"name": "synonomous",
"version": "2.0.0",
"description": "Transform strings into synonyms for list decoration",
"version": "2.0.1",
"description": "Decorate an array instance with synonyms of its elements",
"main": "index.js",

@@ -13,3 +13,6 @@ "scripts": {

},
"author": "Jonathan Eiten <jonathan.eiten@mojotouch.com>",
"author": {
"name": "Jonathan Eiten",
"email": "jonathan.eiten@mojotouch.com"
},
"license": "MIT",

@@ -16,0 +19,0 @@ "bugs": {

# synonomous
Transform strings into synonyms for list decoration
Decorate an array instance with synonyms of its elements
_"... because `synonymous` was already registered!"_
### Get some synonyms
To just get a list of synonyms for a given string using the default selection of "transformer" functions (`verbatim` and `toCamelCase`):
```js
var Synonomous = require('synonomous');
var synonomous = new Synonomous;
synonomous.getSynonyms('background-color');
var Decorator = require('synonomous');
var decorator = new Decorator;
decorator.getSynonyms('background-color');
```

@@ -20,5 +22,3 @@ Returns:

```js
var Synonomous = require('synonomous');
var synonomous = new Synonomous;
synonomous.getSynonyms('background-color', ['toAllCaps']);
decorator.getSynonyms('background-color', ['toAllCaps']);
```

@@ -31,22 +31,21 @@ Returns:

```
#### Custom instance default
#### Setting a custom instance default
To set the default list of "transformer" functions for all subsequent calls from this instance:
```js
var customTransformationsList = ['verbatim', 'toAllCaps'];
synonomous.transformations = customTransformationsList;
decorator.transformations = customTransformationsList;
```
#### Reset instance default
#### Reset instance default to global default
To reset to default:
```js
delete synonomous.transformations; // reveal Synonomous.prototype.transformations
delete decorator.transformations; // reveal Synonymous.prototype.transformations
```
#### Custom shared default
To override default for all subsequent calls from all instances:
To set the default list of "transformer" functions for all subsequent calls from this instance:
#### Setting a custom shared default
To override the shared default for all subsequent calls from all instances:
```js
var saveSharedDefault = synonomous.prototype.transformations; // save for later
synonomous.prototype.transformations = customTransformationsList;
var saveSharedDefault = decorator.prototype.transformations; // save for later
decorator.prototype.transformations = customTransformationsList;
...
// restore shared default later on:
synonomous.prototype.transformations = saveSharedDefault;
decorator.prototype.transformations = saveSharedDefault;
```

@@ -58,26 +57,28 @@

transformers.toQuotation = function(key) { return '"' + key + '"'; }
```
This access to the `transformers` namespace also permits overriding a built-in transformer, although doing so is _not recommended._
### Decorate an object with properties
To add properties all referencing another existing property (_i.e.,_ synonyms):
To add properties to an object, all with the same value (_i.e.,_ synonyms), use the `decorateObject` (aka `decorate`) method:
```js
var myObject = {}, item = {};
var myObject = {}, value = {};
var propNames = ['width', 'thickness'];
synonomous.decorate(myObject, propNames, item);
decorator.decorateObject(myObject, propNames, value);
```
Returns mutated `myObject`:
Mutates and returns `myObject`:
```
{
'width': item,
'thickness': item
'width': value,
'thickness': value
}
```
Note that this function does not call `getSynonyms`; it merely adds properties to `myObject` with the names provided in `propNames` and sets them to `item`.
Note that this function does not call `getSynonyms`; it merely adds properties to `myObject` with the names provided in `propNames` and sets them to `value`.
### Decorate an object property
Rather than adding the properties to `myObject` in the above example, you can also specify a property of `myObject` to decorate instead by defining `synonomous.dictPath` (which is undefined by default).
The target property is expected to be an object. If it does not exist, it will be created as an object (with null prototype).
Rather than adding the properties to `myObject` object directly (_i.e.,_ to the "root" of the object) as in the above example, you can optionally specify a property _of_ `myObject` to decorate instead by defining a path to a "dictionary" property in `decorator.dictPath` (which is undefined by default). The specified dictionary property is expected to be an object. If it does not exist, it is created as an object (with null prototype).
Example:
```js
synonymous.dictPath = 'properties';
synonomous.decorate(myObject, propNames, item);
decorator.dictPath = 'properties';
decorator.decorateObject(myObject, propNames, value);
```

@@ -88,4 +89,4 @@ Returns:

'properties': {
'width': item,
'thickness': item
'width': value,
'thickness': value
}

@@ -96,4 +97,4 @@ }

```js
synonymous.dictPath = 'properties.dimensions'; // or: ['properties'.'dimensions']
synonomous.decorate(myObject, propNames, item);
decorator.dictPath = 'properties.dimensions'; // or: ['properties'.'dimensions']
decorator.decorateObject(myObject, propNames, value);
```

@@ -105,4 +106,4 @@ Returns:

'dimensions': {
'width': item,
'thickness': item
'width': value,
'thickness': value
}

@@ -112,12 +113,16 @@ }

```
It is interesting to note that getting `synonymous.dictPath` always returns an array, but one with a `toString`
override such that when coerced to a string it returns dot-separated values, producing a dot-path.
It is interesting to note that referencing `decorator.dictPath` always returns an array, but one with a `toString`
override such that when the array is coerced to a string it returns a "dot-path," a string containing dot-separated parts:
```js
console.dir(decorator.dictPath); // Array(2) -> ["properties", "dimensions"]
console.log(decorator.dictPath); // "properties.dimension"
```
`synonomous.dictPath` can be customized in the same manner as described above for `synonomous.transformations`.
`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`.
### Decorate an array with synonyms
This is the real utility of the `synonomous` module:
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 synonomous = new Synonomous(['verbatim', 'toAllCaps', 'toCamelCase']);
var decorator = new Synonymous(['verbatim', 'toAllCaps', 'toCamelCase']);
var list = ['borderLeft', 'background-color'];
synonomous.decorateList(list);
decorator.decorateArray(list); // synonym: `decorateList`
```

@@ -136,14 +141,12 @@ 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 properties that point to elements whose keys are transformations of each element. 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.
The above also demonstrates overriding the default selection of "transformer" functions with a different selection by defining a new list in `synonomous.transfomations`, which is exactly what the constructor did with the supplied parameter containing such a list.
#### Decorating with synonyms of a single element
When you just want to decorate your list with synonyms of a single element of the list, you can specify the index of such an element with the overload `decorateList(index: number, list: (string|object)[])`:
When you just want to decorate your list with synonyms of a single element of the list, you can specify the index of such an element with the overload `decorateArray(index: number, list: (string|object)[])`:
```js
var synonomous = new Synonomous;
var decorator = new Synonymous;
var list = ['borderLeft', 'background-color'];
synonomous.decorateList(1, list); // just decorate with synonyms for 2nd element
decorator.decorateArray(1, list); // just decorate with synonyms for 2nd element
```

@@ -161,5 +164,5 @@ `list` now looks like this:

#### 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 `decorateList(list: (string|object)[], propName: string = 'name')`:
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')`:
```js
var synonomous = new Synonomous;
var decorator = new Synonymous;
var list = [

@@ -169,3 +172,3 @@ { style: 'borderLeft', value: '8px' },

];
synonomous.decorateList(list, 'style'); // decorate with synonyms of value of each element's `style` property
decorator.decorateArray(list, 'style'); // decorate with synonyms of each element's `style` property
```

@@ -183,12 +186,12 @@ `list` now looks like this:

Notes:
1. The `propName` parameter defaults to the value of the `synonomous.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 `synonomous.propName` property can be a dot-path (or an array), similar to `synonomous.dictPath`.
3. `synonomous.propName` can be customized in the same manner as described above for `synonomous.dictPath` and `synonomous.transformations`.
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`.
#### Decorating with synonyms of a property of a single element
You can of course combine these two features with the overload `decorateList(index: number, list: object[], propName: string = 'name')`:
You can of course combine these two features with the overload `decorateArray(index: number, list: object[], propName: (string|array) = 'name')`:
```js
synonomous.decorateList(1, list, 'style');
decorator.decorateArray(1, list, 'style');
```
Results same as above but only synonyms of the 2nd element are added to `list` (no `borderLeft` property in this case).
Results same as above but only synonyms of the 2nd element (index `1`) are added to `list` (no `borderLeft` property in this case).

@@ -199,8 +202,18 @@ ### Using synonyms in code

The `verbatim` transformer does not prepend `$`. Any results that are integers that would overwrite existing array indexes are not added as properties.
The `verbatim` transformer does not prepend `$`. Any results that are integers that would overwrite existing array indexes are not added as properties by `decorateArray`.
### Revision History
* **2.0.1** —
* Fixed `decorate` to return the given object itself (instead of the drill-down context within the given object).
* Added two method a.k.a.'s to clarify the intent of each:
* `decorateObject` for `decorate`
* `decorateArray` for `decorateList`
* **2.0.0**
* Added `decorate` method and `dictPath` property.
* Changed `propName` property to `propPath`. _(Breaking change if property used.)_
* Changed optional constructor params to a single `options` object. _(Breaking change if params were used.)_
* Moved transformers to their own file. _(Breaking change if you want to override and exsiting transformer or add a custom transformer.)_
* **1.0.2** — Fixed overloads of `decorateList` when first param `index` is omitted.
* **1.0.1** — Added `toTitle` transformer.
* **1.0.0** — Initial release.
* **2.0.0** - Bug fixes. Added `decorate` method and `dictPath` property. Changed `propName` property to `propPath`. Changed constructor params to a single `options` object.
* **1.0.0** — Initial release.
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