UNPKG

1 kBJavaScriptView Raw
1'use strict';
2
3const balancedMatch = require('balanced-match');
4const styleSearch = require('style-search');
5
6/**
7 * Search a CSS string for functions by name.
8 * For every match, invoke the callback, passing the function's
9 * "argument(s) string" (whatever is inside the parentheses)
10 * as an argument.
11 *
12 * Callback will be called once for every matching function found,
13 * with the function's "argument(s) string" and its starting index
14 * as the arguments.
15 *
16 * @param {string} source
17 * @param {string} functionName
18 * @param {Function} callback
19 */
20module.exports = function (source, functionName, callback) {
21 styleSearch(
22 {
23 source,
24 target: functionName,
25 functionNames: 'check',
26 },
27 (match) => {
28 if (source[match.endIndex] !== '(') {
29 return;
30 }
31
32 const parensMatch = balancedMatch('(', ')', source.substr(match.startIndex));
33
34 if (!parensMatch) {
35 throw new Error(`No parens match: "${source}"`);
36 }
37
38 callback(parensMatch.body, match.endIndex + 1);
39 },
40 );
41};