hastscript
Advanced tools
Comparing version 3.0.0 to 3.0.1
314
index.js
@@ -12,8 +12,3 @@ /** | ||
/* eslint-env commonjs */ | ||
/* | ||
* Dependencies. | ||
*/ | ||
/* Dependencies. */ | ||
var parseSelector = require('hast-util-parse-selector'); | ||
@@ -25,216 +20,177 @@ var camelcase = require('camelcase'); | ||
/* Expose. */ | ||
module.exports = h; | ||
/** | ||
* Parse a (list of) primitives. | ||
* Hyperscript compatible DSL for creating virtual HAST | ||
* trees. | ||
* | ||
* @param {Object} info - Information. | ||
* @param {string} name - Property name. | ||
* @param {*} value - Values to parse. | ||
* @return {*} - Parsed `value`. | ||
* @param {string?} selector - Simple CSS selector to parse. | ||
* @param {Object?} properties - HTML attributes to add. | ||
* @param {string|Array.<string|Node>} children - List of | ||
* children to add. | ||
* @return {Node} - HAST node. | ||
*/ | ||
function parsePrimitive(info, name, value) { | ||
var result = value; | ||
var index; | ||
var length; | ||
function h(selector, properties, children) { | ||
var node = parseSelector(selector); | ||
var property; | ||
if (typeof value === 'object' && 'length' in value) { | ||
length = value.length; | ||
index = -1; | ||
result = []; | ||
if ( | ||
properties && | ||
!children && | ||
( | ||
typeof properties === 'string' || | ||
'length' in properties || | ||
isNode(node.tagName, properties) | ||
) | ||
) { | ||
children = properties; | ||
properties = null; | ||
} | ||
while (++index < length) { | ||
result[index] = parsePrimitive(info, name, value[index]); | ||
} | ||
return result; | ||
if (properties) { | ||
for (property in properties) { | ||
addProperty(node.properties, property, properties[property]); | ||
} | ||
} | ||
if (info.numeric || info.positiveNumeric) { | ||
if (!isNaN(result) && result !== '') { | ||
result = Number(result); | ||
} | ||
} else if (info.boolean || info.overloadedBoolean) { | ||
/* | ||
* Accept `boolean` and `string`. | ||
*/ | ||
addChild(node.children, children); | ||
if ( | ||
typeof result === 'string' && | ||
(result === '' || value.toLowerCase() === name) | ||
) { | ||
result = true; | ||
} | ||
} | ||
return result; | ||
return node; | ||
} | ||
/** | ||
* Add `name` and its `value` to `properties`. | ||
* `properties` can be prefilled by `parseSelector`: | ||
* it can have `id` and `className` properties. | ||
* | ||
* @param {Object} properties - Attributes. | ||
* @param {string} name - Property name. | ||
* @param {*} value - Property value. | ||
*/ | ||
function addProperty(properties, name, value) { | ||
var info = propertyInformation(name) || {}; | ||
var result = value; | ||
var key; | ||
/* Check if `value` is a valid child node of `tagName`. */ | ||
function isNode(tagName, value) { | ||
var type = value.type; | ||
/* | ||
* Ignore nully and NaN values. | ||
*/ | ||
if (typeof type === 'string') { | ||
type = type.toLowerCase(); | ||
} | ||
if (value === null || value === undefined || value !== value) { | ||
return; | ||
} | ||
if (tagName === 'input' || !type || typeof type !== 'string') { | ||
return false; | ||
} | ||
/* Handle values. */ | ||
if (name === 'style') { | ||
/* Accept `object`. */ | ||
if (typeof value !== 'string') { | ||
result = []; | ||
if (typeof value.children === 'object' && 'length' in value.children) { | ||
return true; | ||
} | ||
for (key in value) { | ||
result.push([key, value[key]].join(': ')); | ||
} | ||
if (tagName === 'button') { | ||
return type !== 'menu' && | ||
type !== 'submit' && | ||
type !== 'reset' && | ||
type !== 'button'; | ||
} | ||
result = result.join('; '); | ||
} | ||
} else if (info.spaceSeparated) { | ||
/* Accept both `string` and `Array`. */ | ||
result = typeof value === 'string' ? spaces(result) : result; | ||
return 'value' in value; | ||
} | ||
/* | ||
* Class-names (which can be added both on | ||
* the `selector` and here). | ||
*/ | ||
/* Add `value` as a child to `nodes`. */ | ||
function addChild(nodes, value) { | ||
var index; | ||
var length; | ||
if (name === 'class' && properties.className) { | ||
result = properties.className.concat(result); | ||
} | ||
} else if (info.commaSeparated) { | ||
/* Accept both `string` and `Array`. */ | ||
result = typeof value === 'string' ? commas(result) : result; | ||
if (value === null || value === undefined) { | ||
return; | ||
} | ||
if (typeof value === 'string' || typeof value === 'number') { | ||
value = {type: 'text', value: String(value)}; | ||
} | ||
if (typeof value === 'object' && 'length' in value) { | ||
index = -1; | ||
length = value.length; | ||
while (++index < length) { | ||
addChild(nodes, value[index]); | ||
} | ||
result = parsePrimitive(info, name, result); | ||
return; | ||
} | ||
properties[info.propertyName || camelcase(name)] = result; | ||
if (typeof value !== 'object' || !('type' in value)) { | ||
throw new Error('Expected node, nodes, or string, got `' + value + '`'); | ||
} | ||
nodes.push(value); | ||
} | ||
/** | ||
* Add `value` as a child to `nodes`. | ||
* | ||
* @param {Array.<Node>} nodes - List of siblings. | ||
* @param {string|Node|Array.<string|Node>} value - List of | ||
* children or child to add. | ||
*/ | ||
function addChild(nodes, value) { | ||
var index; | ||
var length; | ||
/* Add `name` and its `value` to `properties`. `properties` can | ||
* be prefilled by `parseSelector`: it can have `id` and `className` | ||
* properties. */ | ||
function addProperty(properties, name, value) { | ||
var info = propertyInformation(name) || {}; | ||
var result = value; | ||
var key; | ||
if (value === null || value === undefined) { | ||
return; | ||
} | ||
/* Ignore nully and NaN values. */ | ||
if (value === null || value === undefined || value !== value) { | ||
return; | ||
} | ||
if (typeof value === 'string' || typeof value === 'number') { | ||
value = { | ||
'type': 'text', | ||
'value': String(value) | ||
}; | ||
} | ||
/* Handle values. */ | ||
if (name === 'style') { | ||
/* Accept `object`. */ | ||
if (typeof value !== 'string') { | ||
result = []; | ||
if (typeof value === 'object' && 'length' in value) { | ||
index = -1; | ||
length = value.length; | ||
for (key in value) { | ||
result.push([key, value[key]].join(': ')); | ||
} | ||
while (++index < length) { | ||
addChild(nodes, value[index]); | ||
} | ||
return; | ||
result = result.join('; '); | ||
} | ||
} else if (info.spaceSeparated) { | ||
/* Accept both `string` and `Array`. */ | ||
result = typeof value === 'string' ? spaces(result) : result; | ||
if (typeof value !== 'object' || !('type' in value)) { | ||
throw new Error( | ||
'Expected node, nodes, or string, got `' + value + '`' | ||
); | ||
/* Class-names (which can be added both on | ||
* the `selector` and here). */ | ||
if (name === 'class' && properties.className) { | ||
result = properties.className.concat(result); | ||
} | ||
} else if (info.commaSeparated) { | ||
/* Accept both `string` and `Array`. */ | ||
result = typeof value === 'string' ? commas(result) : result; | ||
} | ||
nodes.push(value); | ||
result = parsePrimitive(info, name, result); | ||
properties[info.propertyName || camelcase(name)] = result; | ||
} | ||
/** | ||
* Check if `value` is a valid child node of | ||
* `tagName`. | ||
* | ||
* @param {string} tagName - Parent tag-name. | ||
* @param {Object} value - Node or properties like value. | ||
* @return {boolean} - Whether `value` is a node. | ||
*/ | ||
function isNode(tagName, value) { | ||
var type = value.type; | ||
/* Parse a (list of) primitives. */ | ||
function parsePrimitive(info, name, value) { | ||
var result = value; | ||
var index; | ||
var length; | ||
if (tagName === 'input' || !type || typeof type !== 'string') { | ||
return false; | ||
} | ||
if (typeof value === 'object' && 'length' in value) { | ||
length = value.length; | ||
index = -1; | ||
result = []; | ||
if (typeof value.children === 'object' && 'length' in value.children) { | ||
return true; | ||
while (++index < length) { | ||
result[index] = parsePrimitive(info, name, value[index]); | ||
} | ||
if (tagName === 'button') { | ||
return type !== 'menu' && | ||
type !== 'submit' && | ||
type !== 'reset' && | ||
type !== 'button'; | ||
return result; | ||
} | ||
if (info.numeric || info.positiveNumeric) { | ||
if (!isNaN(result) && result !== '') { | ||
result = Number(result); | ||
} | ||
return 'value' in value; | ||
} | ||
/** | ||
* Hyperscript compatible DSL for creating virtual HAST | ||
* trees. | ||
* | ||
* @param {string?} selector - Simple CSS selector to parse. | ||
* @param {Object?} properties - HTML attributes to add. | ||
* @param {string|Array.<string|Node>} children - List of | ||
* children to add. | ||
* @return {Node} - HAST node. | ||
*/ | ||
function h(selector, properties, children) { | ||
var node = parseSelector(selector); | ||
var property; | ||
} else if (info.boolean || info.overloadedBoolean) { | ||
/* Accept `boolean` and `string`. */ | ||
if ( | ||
properties && | ||
!children && | ||
( | ||
typeof properties === 'string' || | ||
'length' in properties || | ||
isNode(node.tagName, properties) | ||
) | ||
typeof result === 'string' && | ||
(result === '' || value.toLowerCase() === name) | ||
) { | ||
children = properties; | ||
properties = null; | ||
result = true; | ||
} | ||
} | ||
if (properties) { | ||
for (property in properties) { | ||
addProperty(node.properties, property, properties[property]); | ||
} | ||
} | ||
addChild(node.children, children); | ||
return node; | ||
return result; | ||
} | ||
/* | ||
* Expose. | ||
*/ | ||
module.exports = h; |
{ | ||
"name": "hastscript", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Hyperscript compatible DSL for creating virtual HAST trees", | ||
@@ -16,9 +16,3 @@ "license": "MIT", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/hastscript.git" | ||
}, | ||
"repository": "https://github.com/wooorm/hastscript", | ||
"bugs": "https://github.com/wooorm/hastscript/issues", | ||
@@ -29,2 +23,5 @@ "author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
@@ -39,15 +36,8 @@ "camelcase": "^3.0.0", | ||
"browserify": "^13.0.0", | ||
"eslint": "^2.0.0", | ||
"esmangle": "^1.0.0", | ||
"hast": "0.0.2", | ||
"istanbul": "^0.4.0", | ||
"jscs": "^3.0.0", | ||
"jscs-jsdoc": "^2.0.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-usage": "^4.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"tape": "^4.0.0" | ||
"nyc": "^8.1.0", | ||
"remark-cli": "^2.0.0", | ||
"remark-preset-wooorm": "^1.0.0", | ||
"tape": "^4.0.0", | ||
"xo": "^0.16.0" | ||
}, | ||
@@ -59,9 +49,28 @@ "scripts": { | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint-api": "eslint .", | ||
"lint-style": "jscs --reporter inline .", | ||
"lint": "npm run lint-api && npm run lint-style", | ||
"test-api": "node test.js", | ||
"test-coverage": "istanbul cover test.js", | ||
"lint": "xo", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.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, | ||
"rules": { | ||
"no-self-compare": "off", | ||
"guard-for-in": "off", | ||
"max-lines": "off" | ||
}, | ||
"ignores": [ | ||
"hastscript.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"output": true, | ||
"presets": "wooorm" | ||
} | ||
} |
# hastscript [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
<!--lint disable heading-increment--> | ||
[Hyperscript][] (and [`virtual-hyperscript`][virtual-hyperscript]) | ||
@@ -16,5 +14,2 @@ compatible DSL for creating virtual [HAST][] trees. | ||
**hastscript** is also available as an AMD, CommonJS, and globals | ||
module, [uncompressed and compressed][releases]. | ||
## Usage | ||
@@ -50,3 +45,3 @@ | ||
properties: { id: 'some-id', className: [ 'foo' ] }, | ||
children: | ||
children: | ||
[ { type: 'element', | ||
@@ -63,3 +58,3 @@ tagName: 'span', | ||
properties: { className: [ 'alpha', 'bravo', 'charlie' ], download: true }, | ||
children: | ||
children: | ||
[ { type: 'text', value: 'delta' }, | ||
@@ -81,6 +76,4 @@ { type: 'text', value: 'echo' } ] } ] } | ||
defaults to a `div` element. | ||
* `properties` (`Object.<string, *>`, optional) | ||
— Map of properties; | ||
* `children` (`string`, `Node`, `Array.<string|Node>`, optional) | ||
@@ -110,4 +103,2 @@ — (List of) child nodes, when strings are encountered, | ||
[releases]: https://github.com/wooorm/hastscript/releases | ||
[license]: LICENSE | ||
@@ -114,0 +105,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7
8930
3
158
112
1
1