From 18d0252ee672401fae12007cb15e1a70c38f0d76 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Sep 2015 12:26:38 +0200 Subject: [PATCH] Initial commit --- .editorconfig | 15 +++++ .eslintignore | 6 ++ .eslintrc | 6 ++ .gitignore | 8 +++ .jscs.json | 145 +++++++++++++++++++++++++++++++++++++++++++ .mdastignore | 2 + .mdastrc | 13 ++++ .travis.yml | 9 +++ LICENSE | 22 +++++++ bower.json | 42 +++++++++++++ component.json | 25 ++++++++ history.md | 6 ++ index.js | 139 +++++++++++++++++++++++++++++++++++++++++ package.json | 62 ++++++++++++++++++ readme.md | 92 +++++++++++++++++++++++++++ test.js | 132 +++++++++++++++++++++++++++++++++++++++ unist-util-is.js | 143 ++++++++++++++++++++++++++++++++++++++++++ unist-util-is.min.js | 1 + 18 files changed, 868 insertions(+) create mode 100644 .editorconfig create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 .jscs.json create mode 100644 .mdastignore create mode 100644 .mdastrc create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 bower.json create mode 100644 component.json create mode 100644 history.md create mode 100644 index.js create mode 100644 package.json create mode 100644 readme.md create mode 100644 test.js create mode 100644 unist-util-is.js create mode 100644 unist-util-is.min.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..658a689 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.{json,html,svg,css,mdastrc,eslintrc}] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..58ca32f --- /dev/null +++ b/.eslintignore @@ -0,0 +1,6 @@ +build/ +components/ +coverage/ +build.js +unist-util-is.js +unist-util-is.min.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..4426f37 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": "eslint:recommended", + "rules": { + "quotes": [2, "single"] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f41859a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +*.log +bower_components/ +build/ +components/ +coverage/ +node_modules/ +build.js diff --git a/.jscs.json b/.jscs.json new file mode 100644 index 0000000..218dbac --- /dev/null +++ b/.jscs.json @@ -0,0 +1,145 @@ +{ + "excludeFiles": [ + "build/", + "components/", + "coverage/", + "node_modules/", + "build.js", + "unist-util-is.js", + "unist-util-is.min.js" + ], + "jsDoc": { + "checkAnnotations": "jsdoc3", + "checkParamNames": true, + "checkRedundantAccess": true, + "checkRedundantParams": true, + "checkRedundantReturns": true, + "checkReturnTypes": true, + "checkTypes": "strictNativeCase", + "enforceExistence": true, + "requireHyphenBeforeDescription": true, + "requireNewlineAfterDescription": true, + "requireParamTypes": true, + "requireParamDescription": true, + "requireReturnTypes": true + }, + "requireCurlyBraces": [ + "if", + "else", + "for", + "while", + "do", + "try", + "catch" + ], + "requireSpaceAfterKeywords": [ + "if", + "else", + "for", + "while", + "do", + "switch", + "return", + "try", + "catch" + ], + "requireSpaceBeforeBlockStatements": true, + "requireParenthesesAroundIIFE": true, + "requireSpacesInConditionalExpression": true, + "requireSpacesInFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInAnonymousFunctionExpression": { + "beforeOpeningRoundBrace": true, + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInNamedFunctionExpression": { + "beforeOpeningRoundBrace": true, + "beforeOpeningCurlyBrace": true + }, + "requireBlocksOnNewline": true, + "disallowEmptyBlocks": true, + "disallowSpacesInsideObjectBrackets": true, + "disallowSpacesInsideArrayBrackets": true, + "disallowSpacesInsideParentheses": true, + "requireSpacesInsideObjectBrackets": "all", + "disallowDanglingUnderscores": true, + "disallowSpaceAfterObjectKeys": true, + "requireCommaBeforeLineBreak": true, + "requireOperatorBeforeLineBreak": [ + "?", + "+", + "-", + "/", + "*", + "=", + "==", + "===", + "!=", + "!==", + ">", + ">=", + "<", + "<=" + ], + "requireSpaceBeforeBinaryOperators": [ + "+", + "-", + "/", + "*", + "=", + "==", + "===", + "!=", + "!==" + ], + "requireSpaceAfterBinaryOperators": [ + "+", + "-", + "/", + "*", + "=", + "==", + "===", + "!=", + "!==" + ], + "disallowSpaceAfterPrefixUnaryOperators": [ + "++", + "--", + "+", + "-", + "~", + "!" + ], + "disallowSpaceBeforePostfixUnaryOperators": [ + "++", + "--" + ], + "disallowImplicitTypeConversion": [ + "numeric", + "boolean", + "binary", + "string" + ], + "requireCamelCaseOrUpperCaseIdentifiers": true, + "disallowKeywords": [ + "with" + ], + "disallowMultipleLineStrings": true, + "disallowMultipleLineBreaks": true, + "validateLineBreaks": "LF", + "validateQuoteMarks": "'", + "disallowMixedSpacesAndTabs": true, + "disallowTrailingWhitespace": true, + "disallowTrailingComma": true, + "disallowKeywordsOnNewLine": [ + "else" + ], + "requireLineFeedAtFileEnd": true, + "maximumLineLength": 78, + "requireCapitalizedConstructors": true, + "safeContextKeyword": "self", + "requireDotNotation": true, + "disallowYodaConditions": true +} diff --git a/.mdastignore b/.mdastignore new file mode 100644 index 0000000..9a674b6 --- /dev/null +++ b/.mdastignore @@ -0,0 +1,2 @@ +bower_components/ +components/ diff --git a/.mdastrc b/.mdastrc new file mode 100644 index 0000000..c5ec5af --- /dev/null +++ b/.mdastrc @@ -0,0 +1,13 @@ +{ + "output": true, + "plugins": [ + "lint", + "github", + "comment-config", + "slug", + "validate-links" + ], + "settings": { + "bullet": "*" + } +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8d45900 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +script: npm run-script test-travis +node_js: +- '0.10' +- '0.11' +- '0.12' +- iojs +sudo: false +after_script: npm install codecov.io && cat ./coverage/lcov.info | codecov diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..32e7a3d --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +(The MIT License) + +Copyright (c) 2015 Titus Wormer + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..4feb36d --- /dev/null +++ b/bower.json @@ -0,0 +1,42 @@ +{ + "name": "unist-util-is", + "main": "unist-util-is.js", + "description": "Utility to check if a node passes a test", + "license": "MIT", + "keywords": [ + "unist", + "mdast", + "markdown", + "retext", + "natural", + "language", + "node", + "is", + "equal", + "test", + "type", + "util", + "utility" + ], + "repository": { + "type": "git", + "url": "https://github.com/wooorm/unist-util-is.git" + }, + "authors": [ + "Titus Wormer " + ], + "ignore": [ + ".*", + "*.log", + "*.md", + "build/", + "components/", + "coverage/", + "node_modules/", + "build.js", + "index.js", + "test.js", + "component.json", + "package.json" + ] +} diff --git a/component.json b/component.json new file mode 100644 index 0000000..7132ba1 --- /dev/null +++ b/component.json @@ -0,0 +1,25 @@ +{ + "name": "unist-util-is", + "version": "0.0.0", + "description": "Utility to check if a node passes a test", + "license": "MIT", + "keywords": [ + "unist", + "mdast", + "markdown", + "retext", + "natural", + "language", + "node", + "is", + "equal", + "test", + "type", + "util", + "utility" + ], + "repository": "wooorm/unist-util-is", + "scripts": [ + "index.js" + ] +} diff --git a/history.md b/history.md new file mode 100644 index 0000000..bf4a386 --- /dev/null +++ b/history.md @@ -0,0 +1,6 @@ + + + + +0.0.0 / 2015-09-02 +================== diff --git a/index.js b/index.js new file mode 100644 index 0000000..f065fea --- /dev/null +++ b/index.js @@ -0,0 +1,139 @@ +/** + * @author Titus Wormer + * @copyright 2015 Titus Wormer + * @license MIT + * @module unist:util:is + * @fileoverview Utility to check if a node passes a test. + */ + +'use strict'; + +/* eslint-env commonjs */ + +/** + * Test. + * + * @typedef {Function} is~test + * @param {Node} node - Node to test. + * @param {number} index - Position of `node` in `parent`. + * @param {Node} parent - Parent of `node`. + * @return {boolean?} - Whether this iteration passes. + */ + +/** + * Utility to return true. + * + * @type {is~test} + */ +function first() { + return true; +} + +/** + * Utility to convert a string into a function which checks + * a given node’s type for said string. + * + * @param {string} test - Node type to test. + * @return {is~test} - Tester. + */ +function typeFactory(test) { + return function (node) { + return Boolean(node && node.type === test); + } +} + +/** + * Utility to convert a node into a function which checks + * a given node for strict equality. + * + * @param {Node} test - Node to test. + * @return {is~test} - Tester. + */ +function nodeFactory(test) { + return function (node) { + return node === test; + } +} + +/** + * Assert if `test` passes for `node`. + * When a `parent` node is known the `index` of node + * + * @example + * is(null, {type: 'strong'}); // true + * + * @example + * is('strong', {type: 'strong'}); // true + * is('emphasis', {type: 'strong'}); // false + * + * @example + * var node = {type: 'strong'}; + * is(node, node) // true + * is(node, {type: 'strong'}) // false + * + * @example + * var node = {type: 'strong'}; + * var parent = {type: 'paragraph', children: [node]}; + * function test(node, n) {return n === 5}; + * is(test, {type: 'strong'}); // false + * is(test, {type: 'strong'}, 4, parent); // false + * is(test, {type: 'strong'}, 5, parent); // true + * + * @example + * var node = {type: 'strong'}; + * var parent = {type: 'paragraph', children: [node]}; + * is('strong'); // throws + * is('strong', node, 0) // throws + * is('strong', node, null, parent) // throws + * is('strong', node, 0, {type: 'paragraph'}) // throws + * is('strong', node, -1, parent) // throws + * is('strong', node, Infinity, parent) // throws + * + * @param {(string|Node|is~test)?} test - Tester. + * @param {Node} node - Node to test. + * @param {number?} [index] - Position of `node` in `parent`. + * @param {Node?} [parent] - Parent of `node`. + * @param {*} [context] - Context to invoke `test` with. + * @return {boolean} - Whether `test` passes. + */ +function is(test, node, index, parent, context) { + var hasParent = parent !== null && parent !== undefined; + var hasIndex = index !== null && index !== undefined; + + if (typeof test === 'string') { + test = typeFactory(test); + } else if (test && test.type) { + test = nodeFactory(test); + } else if (test === null || test === undefined) { + test = first; + } else if (typeof test !== 'function') { + throw new Error('Expected function, string, or node as test'); + } + + if (!node || !node.type) { + throw new Error('Expected node'); + } + + if ( + hasIndex && + (typeof index !== 'number' || index < 0 || index === Infinity) + ) { + throw new Error('Expected positive finite index or child node'); + } + + if (hasParent && (!parent || !parent.type || !parent.children)) { + throw new Error('Expected parent node'); + } + + if (hasParent !== hasIndex) { + throw new Error('Expected both parent and index'); + } + + return Boolean(test.call(context, node, index, parent)); +} + +/* + * Expose. + */ + +module.exports = is; diff --git a/package.json b/package.json new file mode 100644 index 0000000..f1ffb45 --- /dev/null +++ b/package.json @@ -0,0 +1,62 @@ +{ + "name": "unist-util-is", + "version": "0.0.0", + "description": "Utility to check if a node passes a test", + "license": "MIT", + "keywords": [ + "unist", + "mdast", + "markdown", + "retext", + "natural", + "language", + "node", + "is", + "equal", + "test", + "type", + "util", + "utility" + ], + "repository": { + "type": "git", + "url": "https://github.com/wooorm/unist-util-is.git" + }, + "author": { + "name": "Titus Wormer", + "email": "tituswormer@gmail.com" + }, + "files": [ + "index.js", + "LICENSE" + ], + "devDependencies": { + "browserify": "^11.0.0", + "eslint": "^1.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" + }, + "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 unistUtilIs > unist-util-is.js", + "postbundle": "esmangle unist-util-is.js > unist-util-is.min.js", + "build-md": "mdast . --quiet", + "build": "npm run bundle && npm run build-md" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..db4e397 --- /dev/null +++ b/readme.md @@ -0,0 +1,92 @@ +# unist-util-is [![Build Status](https://img.shields.io/travis/wooorm/unist-util-is.svg)](https://travis-ci.org/wooorm/unist-util-is) [![Coverage Status](https://img.shields.io/codecov/c/github/wooorm/unist-util-is.svg)](https://codecov.io/github/wooorm/unist-util-is?branch=master) + +[**Unist**](https://github.com/wooorm/unist) utility to check if a node passes +a test. Useful when working with [**mdast**](https://github.com/wooorm/mdast) +or [**retext**](https://github.com/wooorm/retext). + +## Installation + +[npm](https://docs.npmjs.com/cli/install): + +```bash +npm install unist-util-is +``` + +**unist-util-is** 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](unist-util-is.js) and +[compressed](unist-util-is.min.js). + +## Usage + +```js +var is = require('.'); +var node = { + 'type': 'strong' +}; +var parent = { + 'type': 'paragraph', + 'children': [node] +}; + +function test(node, n) { + return n === 5 +} + +is(null, node); // true +is('strong', node); // true +is('emphasis', node); // false + +is(node, node) // true +is(node, {type: 'strong'}) // false + +is(test, node); // false +is(test, node, 4, parent); // false +is(test, node, 5, parent); // true +``` + +## API + +### is(test, node\[, index, parent\[, context\]\]) + +Utility to check if a node passes a test. + +**Parameters**: + +* `test` ([`Function`](#function-testnode-index-parent), `string`, or + `Node`, optional) + — When not given, return is return `true`. + + Passing a `string` is equal to passing + `function (node) {return node.type === test}`. + + Passing a `node` is equal to passing + `function (node) {return node === test}`. + +* `node` (`Node`) + — [Node](https://github.com/wooorm/unist#unist-nodes) to test; + +* `index` (`number`, optional) — Position of `node` in `parent`; + +* `parent` (`Node`, optional) — Parent of `node`; + +* `context` (`*`, optional) — Parent of `node`. + +**Returns**: `boolean`, whether `test` passed. + +### function test(node\[, index, parent\]) + +**Parameters**: + +* `node` (`Node`) — Node to test; +* `index` (`number`?) — Position of `node` in `parent`. +* `parent` (`Node`?) — Parent of `node`. + +**Context**: The to `is` given context. + +**Returns**: `boolean?`, whether this iteration passes. + +## License + +[MIT](LICENSE) © [Titus Wormer](http://wooorm.com) diff --git a/test.js b/test.js new file mode 100644 index 0000000..65d1e99 --- /dev/null +++ b/test.js @@ -0,0 +1,132 @@ +/** + * @author Titus Wormer + * @copyright 2015 Titus Wormer + * @license MIT + * @module unist:util:is + * @fileoverview Test suite for `unit-util-is`. + */ + +'use strict'; + +/* eslint-env node, mocha */ + +/* + * Dependencies. + */ + +var assert = require('assert'); +var is = require('./'); + +/* + * Methods. + */ + +var throws = assert.throws; +var equal = assert.strictEqual; + +/* + * Tests. + */ + +describe('unist-util-is', function () { + var node = { + 'type': 'strong' + }; + + var parent = { + 'type': 'paragraph', + 'children': [] + }; + + it('should throw when `test` is invalid', function () { + throws(function () { + is(false); + }, /Expected function, string, or node as test/); + }); + + it('should throw when `index` is invalid', function () { + throws(function () { + is(null, node, -1, parent); + }, /Expected positive finite index or child node/); + + throws(function () { + is(null, node, Infinity, parent); + }, /Expected positive finite index or child node/); + + throws(function () { + is(null, node, false, parent); + }, /Expected positive finite index or child node/); + }); + + it('should throw when `parent` is invalid', function () { + throws(function () { + is(null, node, 0, {}); + }, /Expected parent node/); + + throws(function () { + is(null, node, 0, { + 'type': 'paragraph' + }); + }, /Expected parent node/); + }); + + it('should throw `parent` xor `index` are given', function () { + throws(function () { + is(null, node, 0); + }, /Expected both parent and index/); + + throws(function () { + is(null, node, null, parent); + }, /Expected both parent and index/); + }); + + it('should fail without node', function () { + throws(function () { + is(); + }, /Expected node/); + }); + + it('should return true without test', function () { + equal(is(null, node), true); + }); + + it('should match types', function () { + equal(is('strong', node), true); + equal(is('emphasis', node), false); + }); + + it('should match nodes', function () { + equal(is(node, node), true); + equal(is(node, { + 'type': 'strong' + }), false); + }); + + it('should accept a test', function () { + /** Test. */ + function test(node, n) { + return n === 5; + } + + equal(is(test, node), false); + equal(is(test, node, 0, parent), false); + equal(is(test, node, 5, parent), true); + }); + + it('should invoke test', function (done) { + var context = { + 'foo': 'bar' + }; + + /** Test. */ + function test(a, b, c) { + equal(this, context); + equal(a, node); + equal(b, 5); + equal(c, parent); + done(); + } + + is(test, node, 5, parent, context); + }); +}); diff --git a/unist-util-is.js b/unist-util-is.js new file mode 100644 index 0000000..59d97c1 --- /dev/null +++ b/unist-util-is.js @@ -0,0 +1,143 @@ +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.unistUtilIs = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o