Skip to content

Commit 2fee770

Browse files
committed
Refactor package.json
1 parent f41cfc7 commit 2fee770

File tree

3 files changed

+197
-21
lines changed

3 files changed

+197
-21
lines changed

package.json

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,21 @@
55
"license": "MIT",
66
"keywords": [
77
"unist",
8-
"mdast",
9-
"markdown",
10-
"retext",
11-
"natural",
12-
"language",
138
"node",
149
"find",
1510
"before",
1611
"util",
1712
"utility"
1813
],
14+
"repository": "https://github.com/wooorm/unist-util-find-all-before",
15+
"bugs": "https://github.com/wooorm/unist-util-find-all-before/issues",
16+
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)",
17+
"contributors": [
18+
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
19+
],
1920
"dependencies": {
2021
"unist-util-is": "^1.0.0"
2122
},
22-
"repository": {
23-
"type": "git",
24-
"url": "https://github.com/wooorm/unist-util-find-all-before.git"
25-
},
26-
"author": {
27-
"name": "Titus Wormer",
28-
"email": "tituswormer@gmail.com"
29-
},
30-
"files": [
31-
"index.js",
32-
"LICENSE"
33-
],
3423
"devDependencies": {
3524
"browserify": "^11.0.0",
3625
"esmangle": "^1.0.0",
@@ -45,11 +34,11 @@
4534
"xo": "^0.17.1"
4635
},
4736
"scripts": {
48-
"lint": "xo",
49-
"bundle": "browserify index.js --no-builtins -s unistUtilFindAllBefore > unist-util-find-all-before.js",
50-
"postbundle": "esmangle unist-util-find-all-before.js > unist-util-find-all-before.min.js",
5137
"build-md": "mdast . --quiet",
52-
"build": "npm run bundle && npm run build-md",
38+
"build-bundle": "browserify index.js --no-builtins -s unistUtilFindAllBefore > unist-util-find-all-before.js",
39+
"build-mangle": "esmangle unist-util-find-all-before.js > unist-util-find-all-before.min.js",
40+
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
41+
"lint": "xo",
5342
"test-api": "node test",
5443
"test-coverage": "nyc --reporter lcov tape test.js",
5544
"test": "npm run build && npm run lint && npm run test-coverage"

unist-util-find-all-before.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
(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.unistUtilFindAllBefore = 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<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2+
'use strict';
3+
4+
var is = require('unist-util-is');
5+
6+
module.exports = findAllBefore;
7+
8+
/* Find nodes before `index` in `parent` which pass `test`. */
9+
function findAllBefore(parent, index, test) {
10+
var results = [];
11+
var children;
12+
var child;
13+
14+
if (!parent || !parent.type || !parent.children) {
15+
throw new Error('Expected parent node');
16+
}
17+
18+
children = parent.children;
19+
20+
if (index && index.type) {
21+
index = children.indexOf(index);
22+
}
23+
24+
if (isNaN(index) || index < 0 || index === Infinity) {
25+
throw new Error('Expected positive finite index or child node');
26+
}
27+
28+
/* Performance. */
29+
if (index > children.length) {
30+
index = children.length;
31+
}
32+
33+
while (index--) {
34+
child = children[index];
35+
36+
if (is(test, child, index, parent)) {
37+
results.push(child);
38+
}
39+
}
40+
41+
return results;
42+
}
43+
44+
},{"unist-util-is":2}],2:[function(require,module,exports){
45+
/**
46+
* @author Titus Wormer
47+
* @copyright 2015 Titus Wormer
48+
* @license MIT
49+
* @module unist:util:is
50+
* @fileoverview Utility to check if a node passes a test.
51+
*/
52+
53+
'use strict';
54+
55+
/* eslint-env commonjs */
56+
57+
/**
58+
* Test.
59+
*
60+
* @typedef {Function} is~test
61+
* @param {Node} node - Node to test.
62+
* @param {number} index - Position of `node` in `parent`.
63+
* @param {Node} parent - Parent of `node`.
64+
* @return {boolean?} - Whether this iteration passes.
65+
*/
66+
67+
/**
68+
* Utility to return true.
69+
*
70+
* @type {is~test}
71+
*/
72+
function first() {
73+
return true;
74+
}
75+
76+
/**
77+
* Utility to convert a string into a function which checks
78+
* a given node’s type for said string.
79+
*
80+
* @param {string} test - Node type to test.
81+
* @return {is~test} - Tester.
82+
*/
83+
function typeFactory(test) {
84+
return function (node) {
85+
return Boolean(node && node.type === test);
86+
}
87+
}
88+
89+
/**
90+
* Utility to convert a node into a function which checks
91+
* a given node for strict equality.
92+
*
93+
* @param {Node} test - Node to test.
94+
* @return {is~test} - Tester.
95+
*/
96+
function nodeFactory(test) {
97+
return function (node) {
98+
return node === test;
99+
}
100+
}
101+
102+
/**
103+
* Assert if `test` passes for `node`.
104+
* When a `parent` node is known the `index` of node
105+
*
106+
* @example
107+
* is(null, {type: 'strong'}); // true
108+
*
109+
* @example
110+
* is('strong', {type: 'strong'}); // true
111+
* is('emphasis', {type: 'strong'}); // false
112+
*
113+
* @example
114+
* var node = {type: 'strong'};
115+
* is(node, node) // true
116+
* is(node, {type: 'strong'}) // false
117+
*
118+
* @example
119+
* var node = {type: 'strong'};
120+
* var parent = {type: 'paragraph', children: [node]};
121+
* function test(node, n) {return n === 5};
122+
* is(test, {type: 'strong'}); // false
123+
* is(test, {type: 'strong'}, 4, parent); // false
124+
* is(test, {type: 'strong'}, 5, parent); // true
125+
*
126+
* @example
127+
* var node = {type: 'strong'};
128+
* var parent = {type: 'paragraph', children: [node]};
129+
* is('strong'); // throws
130+
* is('strong', node, 0) // throws
131+
* is('strong', node, null, parent) // throws
132+
* is('strong', node, 0, {type: 'paragraph'}) // throws
133+
* is('strong', node, -1, parent) // throws
134+
* is('strong', node, Infinity, parent) // throws
135+
*
136+
* @param {(string|Node|is~test)?} test - Tester.
137+
* @param {Node} node - Node to test.
138+
* @param {number?} [index] - Position of `node` in `parent`.
139+
* @param {Node?} [parent] - Parent of `node`.
140+
* @param {*} [context] - Context to invoke `test` with.
141+
* @return {boolean} - Whether `test` passes.
142+
*/
143+
function is(test, node, index, parent, context) {
144+
var hasParent = parent !== null && parent !== undefined;
145+
var hasIndex = index !== null && index !== undefined;
146+
147+
if (typeof test === 'string') {
148+
test = typeFactory(test);
149+
} else if (test && test.type) {
150+
test = nodeFactory(test);
151+
} else if (test === null || test === undefined) {
152+
test = first;
153+
} else if (typeof test !== 'function') {
154+
throw new Error('Expected function, string, or node as test');
155+
}
156+
157+
if (!node || !node.type) {
158+
throw new Error('Expected node');
159+
}
160+
161+
if (
162+
hasIndex &&
163+
(typeof index !== 'number' || index < 0 || index === Infinity)
164+
) {
165+
throw new Error('Expected positive finite index or child node');
166+
}
167+
168+
if (hasParent && (!parent || !parent.type || !parent.children)) {
169+
throw new Error('Expected parent node');
170+
}
171+
172+
if (hasParent !== hasIndex) {
173+
throw new Error('Expected both parent and index');
174+
}
175+
176+
return Boolean(test.call(context, node, index, parent));
177+
}
178+
179+
/*
180+
* Expose.
181+
*/
182+
183+
module.exports = is;
184+
185+
},{}]},{},[1])(1)
186+
});

unist-util-find-all-before.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)