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

nlcst-is-literal

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nlcst-is-literal - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

407

index.js

@@ -1,290 +0,201 @@

/**
* @author Titus Wormer
* @copyright 2014-2015 Titus Wormer
* @license MIT
* @module nlcst:is-literal
* @fileoverview Check whether an NLCST node is meant literally.
*/
'use strict';
/* eslint-env commonjs */
/*
* Dependencies.
*/
var toString = require('nlcst-to-string');
/*
* Single delimiters.
*/
module.exports = isLiteral;
var single = {
'-': true, // hyphen-minus
'–': true, // en-dash
'—': true, // em-dash
':': true, // colon
';': true // semicolon
'-': true, // hyphen-minus
'–': true, // en-dash
'—': true, // em-dash
':': true, // colon
';': true // semicolon
};
/*
* Pair delimiters. From common sense, and wikipedia:
* Mostly from https://en.wikipedia.org/wiki/Quotation_mark.
*/
/* Pair delimiters. From common sense, and wikipedia:
* Mostly from https://en.wikipedia.org/wiki/Quotation_mark. */
var pairs = {
',': {
',': true
},
'-': {
'-': true
},
'–': {
'–': true
},
'—': {
'—': true
},
'"': {
'"': true
},
'\'': {
'\'': true
},
'‘': {
'’': true
},
'‚': {
'’': true
},
'’': {
'’': true,
'‚': true
},
'“': {
'”': true
},
'”': {
'”': true
},
'„': {
'”': true,
'“': true
},
'«': {
'»': true
},
'»': {
'«': true
},
'‹': {
'›': true
},
'›': {
'‹': true
},
'(': {
')': true
},
'[': {
']': true
},
'{': {
'}': true
},
'⟨': {
'⟩': true
},
'「': {
'」': true
}
}
',': {
',': true
},
'-': {
'-': true
},
'–': {
'–': true
},
'—': {
'—': true
},
'"': {
'"': true
},
'\'': {
'\'': true
},
'‘': {
'’': true
},
'‚': {
'’': true
},
'’': {
'’': true,
'‚': true
},
'“': {
'”': true
},
'”': {
'”': true
},
'„': {
'”': true,
'“': true
},
'«': {
'»': true
},
'»': {
'«': true
},
'‹': {
'›': true
},
'›': {
'‹': true
},
'(': {
')': true
},
'[': {
']': true
},
'{': {
'}': true
},
'⟨': {
'⟩': true
},
'「': {
'」': true
}
};
/**
* Check whether parent contains word-nodes between
* `start` and `end`.
*
* @param {NLCSTParentNode} parent - Node with children.
* @param {number} start - Starting point (inclusive).
* @param {number} end - Ending point (exclusive).
* @return {boolean} - Whether word-nodes are found.
*/
function containsWord(parent, start, end) {
var siblings = parent.children;
var index = start - 1;
/* Check if the node in `parent` at `position` is enclosed
* by matching delimiters. */
function isLiteral(parent, index) {
if (!(parent && parent.children)) {
throw new Error('Parent must be a node');
}
while (++index < end) {
if (siblings[index].type === 'WordNode') {
return true;
}
}
if (isNaN(index)) {
throw new Error('Index must be a number');
}
return false;
}
if (
(!hasWordsBefore(parent, index) && nextDelimiter(parent, index, single)) ||
(!hasWordsAfter(parent, index) && previousDelimiter(parent, index, single)) ||
isWrapped(parent, index, pairs)
) {
return true;
}
/**
* Check if there are word nodes before `position`
* in `parent`.
*
* @param {NLCSTParentNode} parent - Node with children.
* @param {number} position - Position before which to
* check.
* @return {boolean} - Whether word-nodes are found.
*/
function hasWordsBefore(parent, position) {
return containsWord(parent, 0, position);
return false;
}
/**
* Check if there are word nodes before `position`
* in `parent`.
*
* @param {NLCSTParentNode} parent - Node with children.
* @param {number} position - Position afyer which to
* check.
* @return {boolean} - Whether word-nodes are found.
*/
function hasWordsAfter(parent, position) {
return containsWord(parent, position + 1, parent.children.length);
/* Check if the node in `parent` at `position` is enclosed
* by matching delimiters. */
function isWrapped(parent, position, delimiters) {
var prev = previousDelimiter(parent, position, delimiters);
var next;
if (prev) {
next = nextDelimiter(parent, position, delimiters[toString(prev)]);
}
return Boolean(next);
}
/**
* Check if `node` is in `delimiters`.
*
* @param {Node} node - Node to check.
* @param {Object} delimiters - Map of delimiters.
* @return {(Node|boolean)?} - `false` if not, the given
* node when true, and `null` when this is a white-space
* node.
*/
function delimiterCheck(node, delimiters) {
var type = node.type;
/* Find the previous delimiter before `position` in
* `parent`. Returns the delimiter node when found. */
function previousDelimiter(parent, position, delimiters) {
var siblings = parent.children;
var index = position;
var result;
if (type === 'WordNode' || type === 'SourceNode') {
return false;
}
while (index--) {
result = delimiterCheck(siblings[index], delimiters);
if (type === 'WhiteSpaceNode') {
return null;
if (result === null) {
continue;
}
return toString(node) in delimiters ? node : false;
return result;
}
return null;
}
/**
* Find the next delimiter after `position` in
* `parent`. Returns the delimiter node when found.
*
* @param {NLCSTParentNode} parent - Parent to search.
* @param {number} position - Position to search after.
* @param {Object} delimiters - Map of delimiters.
* @return {Node?} - Following delimiter.
*/
/* Find the next delimiter after `position` in
* `parent`. Returns the delimiter node when found. */
function nextDelimiter(parent, position, delimiters) {
var siblings = parent.children;
var index = position;
var length = siblings.length;
var result;
var siblings = parent.children;
var index = position;
var length = siblings.length;
var result;
while (++index < length) {
result = delimiterCheck(siblings[index], delimiters);
while (++index < length) {
result = delimiterCheck(siblings[index], delimiters);
if (result === null) {
continue;
}
return result;
if (result === null) {
continue;
}
return null;
return result;
}
return null;
}
/**
* Find the previous delimiter before `position` in
* `parent`. Returns the delimiter node when found.
*
* @param {NLCSTParentNode} parent - Parent to search.
* @param {number} position - Position to search before.
* @param {Object} delimiters - Map of delimiters.
* @return {Node?} - Previous delimiter.
*/
function previousDelimiter(parent, position, delimiters) {
var siblings = parent.children;
var index = position;
var result;
/* Check if `node` is in `delimiters`. */
function delimiterCheck(node, delimiters) {
var type = node.type;
while (index--) {
result = delimiterCheck(siblings[index], delimiters);
if (type === 'WordNode' || type === 'SourceNode') {
return false;
}
if (result === null) {
continue;
}
if (type === 'WhiteSpaceNode') {
return null;
}
return result;
}
return toString(node) in delimiters ? node : false;
}
return null;
/* Check if there are word nodes before `position`
* in `parent`. */
function hasWordsBefore(parent, position) {
return containsWord(parent, 0, position);
}
/**
* Check if the node in `parent` at `position` is enclosed
* by matching delimiters.
*
* @param {NLCSTParentNode} parent - Parent to search.
* @param {number} position - Position of node to check.
* @param {Object} delimiters - Map of delimiters.
* @return {boolean} - Whether the node is wrapped.
*/
function isWrapped(parent, position, delimiters) {
var prev = previousDelimiter(parent, position, delimiters);
var next;
if (prev) {
next = nextDelimiter(parent, position, delimiters[toString(prev)]);
}
return Boolean(next);
/* Check if there are word nodes before `position`
* in `parent`. */
function hasWordsAfter(parent, position) {
return containsWord(parent, position + 1, parent.children.length);
}
/**
* Check if the node in `parent` at `position` is enclosed
* by matching delimiters.
*
* For example, in:
*
* - `Foo - is meant as a literal.`;
* - `Meant as a literal is - foo.`;
* - `The word “foo” is meant as a literal.`;
*
* ...`foo` is literal.
*
* @param {NLCSTParentNode} parent - Parent to search.
* @param {number} index - Position of node to check.
* @return {boolean} - Whether the node is wrapped.
*/
function isLiteral(parent, index) {
if (!(parent && parent.children)) {
throw new Error('Parent must be a node');
}
/* Check if parent contains word-nodes between
* `start` and `end`. */
function containsWord(parent, start, end) {
var siblings = parent.children;
var index = start - 1;
if (isNaN(index)) {
throw new Error('Index must be a number');
while (++index < end) {
if (siblings[index].type === 'WordNode') {
return true;
}
}
if (
(!hasWordsBefore(parent, index) && nextDelimiter(parent, index, single)) ||
(!hasWordsAfter(parent, index) && previousDelimiter(parent, index, single)) ||
isWrapped(parent, index, pairs)
) {
return true;
}
return false;
return false;
}
/*
* Expose.
*/
module.exports = isLiteral;
{
"name": "nlcst-is-literal",
"version": "1.0.0",
"version": "1.1.0",
"description": "Check whether an NLCST node is meant literally",

@@ -14,41 +14,50 @@ "license": "MIT",

],
"dependencies": {
"nlcst-to-string": "^1.0.0"
},
"repository": "wooorm/nlcst-is-literal",
"author": "Titus Wormer <tituswormer@gmail.com>",
"repository": "https://github.com/wooorm/nlcst-is-literal",
"bugs": "https://github.com/wooorm/nlcst-is-literal/issues",
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
],
"files": [
"index.js"
],
"dependencies": {
"nlcst-to-string": "^2.0.0"
},
"devDependencies": {
"browserify": "^11.0.0",
"eslint": "^1.0.0",
"browserify": "^13.0.0",
"esmangle": "^1.0.0",
"istanbul": "^0.3.0",
"jscs": "^2.0.0",
"jscs-jsdoc": "^1.0.0",
"mdast": "^1.0.0",
"mdast-comment-config": "^1.0.0",
"mdast-github": "^1.0.0",
"mdast-lint": "^1.0.0",
"mdast-slug": "^1.0.0",
"mdast-validate-links": "^1.0.0",
"mocha": "^2.0.0",
"retext": "^1.0.0-rc.3",
"unist-util-visit": "^1.0.0"
"nyc": "^9.0.1",
"remark-cli": "^2.1.0",
"remark-preset-wooorm": "^1.0.0",
"retext": "^4.0.0",
"tape": "^4.6.2",
"unist-util-visit": "^1.0.0",
"xo": "^0.17.1"
},
"scripts": {
"test-api": "mocha --check-leaks test.js",
"test-coverage": "istanbul cover _mocha -- test.js",
"test-travis": "npm run test-coverage",
"test": "npm run test-api",
"lint-api": "eslint .",
"lint-style": "jscs --reporter inline .",
"lint": "npm run lint-api && npm run lint-style",
"make": "npm run lint && npm run test-coverage",
"bundle": "browserify index.js --no-builtins -s nlcstIsLiteral > nlcst-is-literal.js",
"postbundle": "esmangle nlcst-is-literal.js > nlcst-is-literal.min.js",
"build-md": "mdast . --quiet",
"build": "npm run bundle && npm run build-md"
"build-md": "remark . --quiet --frail --output",
"build-bundle": "browserify index.js --bare -s nlcstIsLiteral > nlcst-is-literal.js",
"build-mangle": "esmangle < nlcst-is-literal.js > nlcst-is-literal.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run build && npm run lint && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"xo": {
"space": true,
"ignore": [
"nlcst-is-literal.js"
]
},
"remarkConfig": {
"presets": "wooorm"
}
}

@@ -1,4 +0,4 @@

# nlcst-is-literal [![Build Status](https://img.shields.io/travis/wooorm/nlcst-is-literal.svg)](https://travis-ci.org/wooorm/nlcst-is-literal) [![Coverage Status](https://img.shields.io/codecov/c/github/wooorm/nlcst-is-literal.svg)](https://codecov.io/github/wooorm/nlcst-is-literal)
# nlcst-is-literal [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
Check whether an NLCST node is meant literally. Useful if a tool wants to
Check if an NLCST node is meant literally. Useful if a tool wants to
exclude these values possibly void of meaning.

@@ -11,3 +11,3 @@

[npm](https://docs.npmjs.com/cli/install):
[npm][]:

@@ -18,7 +18,2 @@ ```bash

**nlcst-is-literal** is also available for [bower](http://bower.io/#install-packages),
[component](https://github.com/componentjs/component), and
[duo](http://duojs.org/#getting-started), and as an AMD, CommonJS, and globals
module, [uncompressed](nlcst-is-literal.js) and [compressed](nlcst-is-literal.min.js).
## Usage

@@ -30,19 +25,23 @@

var toString = require('nlcst-to-string');
var isLiteral = require('.');
var literal = require('nlcst-is-literal');
retext().use(function () {
return function (cst) {
visit(cst, 'WordNode', function (node, index, parent) {
if (isLiteral(parent, index)) {
console.log(toString(node));
}
});
retext().use(plugin).process([
'The word “foo” is meant as a literal.',
'The word «bar» is meant as a literal.',
'The word (baz) is meant as a literal.',
'The word, qux, is meant as a literal.',
'The word — quux — is meant as a literal.'
].join('\n\n'));
function plugin() {
return transformer;
function transformer(tree) {
visit(tree, 'WordNode', visitor);
}
function visitor(node, index, parent) {
if (literal(parent, index)) {
console.log(toString(node));
}
}).process([
'The word “foo” is meant as a literal.',
'The word «bar» is meant as a literal.',
'The word (baz) is meant as a literal.',
'The word, qux, is meant as a literal.',
'The word — quux — is meant as a literal.'
].join('\n\n'));
}
}
```

@@ -62,3 +61,3 @@

### isLiteral(parent, index)
### `isLiteral(parent, index)`

@@ -76,20 +75,20 @@ Check if the node in `parent` at `position` is enclosed

**Parameters**
## License
* `node` ([`NLCSTParentNode`](https://github.com/wooorm/nlcst#parent))
— Parent to search.
[MIT][license] © [Titus Wormer][author]
* `nodes` (`Array.<NLCSTNode>`) — Position of node to check.
<!-- Definitions -->
**Returns**
[travis-badge]: https://img.shields.io/travis/wooorm/nlcst-is-literal.svg
`boolean` — Whether the node is literal.
[travis]: https://travis-ci.org/wooorm/nlcst-is-literal
**Throws**
[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/nlcst-is-literal.svg
* `Error` — When no parent node is given;
* `Error` — When no index is given.
[codecov]: https://codecov.io/github/wooorm/nlcst-is-literal
## License
[npm]: https://docs.npmjs.com/cli/install
[MIT](LICENSE) © [Titus Wormer](http://wooorm.com)
[license]: LICENSE
[author]: http://wooorm.com
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