From 40d071ee354fc42c0cc3e3ef3357a8992d86c176 Mon Sep 17 00:00:00 2001 From: akvlad Date: Fri, 6 Sep 2024 00:12:38 +0300 Subject: [PATCH] drop handlebars-helpers; --- lib/handlebars-helpers/index.js | 57 ++ lib/handlebars-helpers/math.js | 288 ++++++++++ lib/handlebars-helpers/string.js | 769 +++++++++++++++++++++++++++ lib/handlebars-helpers/utils.js | 50 ++ lib/handlers/prom_series.js | 3 +- package.json | 4 +- parser/registry/line_format/index.js | 2 +- test/e2e | 2 +- 8 files changed, 1169 insertions(+), 6 deletions(-) create mode 100644 lib/handlebars-helpers/index.js create mode 100644 lib/handlebars-helpers/math.js create mode 100644 lib/handlebars-helpers/string.js create mode 100644 lib/handlebars-helpers/utils.js diff --git a/lib/handlebars-helpers/index.js b/lib/handlebars-helpers/index.js new file mode 100644 index 00000000..fc45b6d9 --- /dev/null +++ b/lib/handlebars-helpers/index.js @@ -0,0 +1,57 @@ +/*! + * handlebars-helpers + * + * Copyright (c) 2013-2017, Jon Schlinkert, Brian Woodward. + * Released under the MIT License. + */ + +'use strict'; + +var lib = { + math: require('./math'), + string: require('./string'), +} + +/** + * Expose helpers + */ + +module.exports = function helpers(groups, options) { + if (typeof groups === 'string') { + groups = [groups]; + } else if (!Array.isArray(groups)) { + options = groups; + groups = null; + } + + options = options || {}; + const hbs = options.handlebars || options.hbs || require('handlebars'); + module.exports.handlebars = hbs; + + if (groups) { + groups.forEach(function(key) { + hbs.registerHelper(lib[key]); + }); + } else { + Object.values(lib).forEach(function(group) { + hbs.registerHelper(group); + }); + } + + return hbs.helpers; +}; + +/** + * Expose helper groups + */ + +Object.entries(lib).forEach(function(key_group) { + const [key, group] = key_group; + module.exports[key] = function(options) { + options = options || {}; + let hbs = options.handlebars || options.hbs || require('handlebars'); + module.exports.handlebars = hbs; + hbs.registerHelper(group); + return hbs.helpers; + }; +}); diff --git a/lib/handlebars-helpers/math.js b/lib/handlebars-helpers/math.js new file mode 100644 index 00000000..f4c138b9 --- /dev/null +++ b/lib/handlebars-helpers/math.js @@ -0,0 +1,288 @@ +'use strict'; + +var isNumber = require('is-number'); +var utils = require('./utils'); +var helpers = module.exports; + +/** + * Return the magnitude of `a`. + * + * @param {Number} `a` + * @return {Number} + * @api public + */ + +helpers.abs = function(num) { + if (!isNumber(num)) { + throw new TypeError('expected a number'); + } + return Math.abs(num); +}; + +/** + * Return the sum of `a` plus `b`. + * + * @param {Number} `a` + * @param {Number} `b` + * @return {Number} + * @api public + */ + +helpers.add = function(a, b) { + if (isNumber(a) && isNumber(b)) { + return Number(a) + Number(b); + } + if (typeof a === 'string' && typeof b === 'string') { + return a + b; + } + return ''; +}; + +/** + * Returns the average of all numbers in the given array. + * + * ```handlebars + * {{avg "[1, 2, 3, 4, 5]"}} + * + * ``` + * + * @param {Array} `array` Array of numbers to add up. + * @return {Number} + * @api public + */ + +helpers.avg = function() { + var args = [].concat.apply([], arguments); + // remove handlebars options object + args.pop(); + return helpers.sum(args) / args.length; +}; + +/** + * Get the `Math.ceil()` of the given value. + * + * @param {Number} `value` + * @return {Number} + * @api public + */ + +helpers.ceil = function(num) { + if (!isNumber(num)) { + throw new TypeError('expected a number'); + } + return Math.ceil(num); +}; + +/** + * Divide `a` by `b` + * + * @param {Number} `a` numerator + * @param {Number} `b` denominator + * @api public + */ + +helpers.divide = function(a, b) { + if (!isNumber(a)) { + throw new TypeError('expected the first argument to be a number'); + } + if (!isNumber(b)) { + throw new TypeError('expected the second argument to be a number'); + } + return Number(a) / Number(b); +}; + +/** + * Get the `Math.floor()` of the given value. + * + * @param {Number} `value` + * @return {Number} + * @api public + */ + +helpers.floor = function(num) { + if (!isNumber(num)) { + throw new TypeError('expected a number'); + } + return Math.floor(num); +}; + +/** + * Return the difference of `a` minus `b`. + * + * @param {Number} `a` + * @param {Number} `b` + * @alias subtract + * @api public + */ + +helpers.minus = function(a, b) { + if (!isNumber(a)) { + throw new TypeError('expected the first argument to be a number'); + } + if (!isNumber(b)) { + throw new TypeError('expected the second argument to be a number'); + } + return Number(a) - Number(b); +}; + +/** + * Get the remainder of a division operation. + * + * @param {Number} `a` + * @param {Number} `b` + * @return {Number} + * @api public + */ + +helpers.modulo = function(a, b) { + if (!isNumber(a)) { + throw new TypeError('expected the first argument to be a number'); + } + if (!isNumber(b)) { + throw new TypeError('expected the second argument to be a number'); + } + return Number(a) % Number(b); +}; + +/** + * Return the product of `a` times `b`. + * + * @param {Number} `a` factor + * @param {Number} `b` multiplier + * @return {Number} + * @alias times + * @api public + */ + +helpers.multiply = function(a, b) { + if (!isNumber(a)) { + throw new TypeError('expected the first argument to be a number'); + } + if (!isNumber(b)) { + throw new TypeError('expected the second argument to be a number'); + } + return Number(a) * Number(b); +}; + +/** + * Add `a` by `b`. + * + * @param {Number} `a` factor + * @param {Number} `b` multiplier + * @api public + */ + +helpers.plus = function(a, b) { + if (!isNumber(a)) { + throw new TypeError('expected the first argument to be a number'); + } + if (!isNumber(b)) { + throw new TypeError('expected the second argument to be a number'); + } + return Number(a) + Number(b); +}; + +/** + * Generate a random number between two values + * + * @param {Number} `min` + * @param {Number} `max` + * @return {String} + * @api public + */ + +helpers.random = function(min, max) { + if (!isNumber(min)) { + throw new TypeError('expected minimum to be a number'); + } + if (!isNumber(max)) { + throw new TypeError('expected maximum to be a number'); + } + return utils.random(min, max); +}; + +/** + * Get the remainder when `a` is divided by `b`. + * + * @param {Number} `a` a + * @param {Number} `b` b + * @api public + */ + +helpers.remainder = function(a, b) { + return a % b; +}; + +/** + * Round the given number. + * + * @param {Number} `number` + * @return {Number} + * @api public + */ + +helpers.round = function(num) { + if (!isNumber(num)) { + throw new TypeError('expected a number'); + } + return Math.round(num); +}; + +/** + * Return the product of `a` minus `b`. + * + * @param {Number} `a` + * @param {Number} `b` + * @return {Number} + * @alias minus + * @api public + */ + +helpers.subtract = function(a, b) { + if (!isNumber(a)) { + throw new TypeError('expected the first argument to be a number'); + } + if (!isNumber(b)) { + throw new TypeError('expected the second argument to be a number'); + } + return Number(a) - Number(b); +}; + +/** + * Returns the sum of all numbers in the given array. + * + * ```handlebars + * {{sum "[1, 2, 3, 4, 5]"}} + * + * ``` + * @param {Array} `array` Array of numbers to add up. + * @return {Number} + * @api public + */ + +helpers.sum = function() { + var args = [].concat.apply([], arguments); + var len = args.length; + var sum = 0; + + while (len--) { + if (utils.isNumber(args[len])) { + sum += Number(args[len]); + } + } + return sum; +}; + +/** + * Multiply number `a` by number `b`. + * + * @param {Number} `a` factor + * @param {Number} `b` multiplier + * @return {Number} + * @alias multiply + * @api public + */ + +helpers.times = function() { + return helpers.multiply.apply(this, arguments); +}; diff --git a/lib/handlebars-helpers/string.js b/lib/handlebars-helpers/string.js new file mode 100644 index 00000000..5f079766 --- /dev/null +++ b/lib/handlebars-helpers/string.js @@ -0,0 +1,769 @@ +'use strict'; + +var isNumber = require('is-number'); +var util = require('handlebars-utils'); +var utils = require('./utils'); +var helpers = module.exports; + +/** + * Append the specified `suffix` to the given string. + * + * ```handlebars + * + * {{append item.stem ".html"}} + * + * ``` + * @param {String} `str` + * @param {String} `suffix` + * @return {String} + * @api public + */ + +helpers.append = function(str, suffix) { + if (typeof str === 'string' && typeof suffix === 'string') { + return str + suffix; + } + return str; +}; + +/** + * camelCase the characters in the given `string`. + * + * ```handlebars + * {{camelcase "foo bar baz"}}; + * + * ``` + * @param {String} `string` The string to camelcase. + * @return {String} + * @api public + */ + +helpers.camelcase = function(str) { + if (!util.isString(str)) return ''; + return utils.changecase(str, function(ch) { + return ch.toUpperCase(); + }); +}; + +/** + * Capitalize the first word in a sentence. + * + * ```handlebars + * {{capitalize "foo bar baz"}} + * + * ``` + * @param {String} `str` + * @return {String} + * @api public + */ + +helpers.capitalize = function(str) { + if (!util.isString(str)) return ''; + return str.charAt(0).toUpperCase() + str.slice(1); +}; + +/** + * Capitalize all words in a string. + * + * ```handlebars + * {{capitalizeAll "foo bar baz"}} + * + * ``` + * @param {String} `str` + * @return {String} + * @api public + */ + +helpers.capitalizeAll = function(str) { + if (!util.isString(str)) return ''; + if (util.isString(str)) { + return str.replace(/\w\S*/g, function(word) { + return helpers.capitalize(word); + }); + } +}; + +/** + * Center a string using non-breaking spaces + * + * @param {String} `str` + * @param {String} `spaces` + * @return {String} + * @api public + */ + +helpers.center = function(str, spaces) { + if (!util.isString(str)) return ''; + var space = ''; + var i = 0; + while (i < spaces) { + space += ' '; + i++; + } + return space + str + space; +}; + +/** + * Like trim, but removes both extraneous whitespace **and + * non-word characters** from the beginning and end of a string. + * + * ```handlebars + * {{chop "_ABC_"}} + * + * + * {{chop "-ABC-"}} + * + * + * {{chop " ABC "}} + * + * ``` + * @param {String} `string` The string to chop. + * @return {String} + * @api public + */ + +helpers.chop = function(str) { + if (!util.isString(str)) return ''; + return utils.chop(str); +}; + +/** + * dash-case the characters in `string`. Replaces non-word + * characters and periods with hyphens. + * + * ```handlebars + * {{dashcase "a-b-c d_e"}} + * + * ``` + * @param {String} `string` + * @return {String} + * @api public + */ + +helpers.dashcase = function(str) { + if (!util.isString(str)) return ''; + return utils.changecase(str, function(ch) { + return '-' + ch; + }); +}; + +/** + * dot.case the characters in `string`. + * + * ```handlebars + * {{dotcase "a-b-c d_e"}} + * + * ``` + * @param {String} `string` + * @return {String} + * @api public + */ + +helpers.dotcase = function(str) { + if (!util.isString(str)) return ''; + return utils.changecase(str, function(ch) { + return '.' + ch; + }); +}; + +/** + * Lowercase all of the characters in the given string. Alias for [lowercase](#lowercase). + * + * ```handlebars + * {{downcase "aBcDeF"}} + * + * ``` + * @param {String} `string` + * @return {String} + * @alias lowercase + * @api public + */ + +helpers.downcase = function() { + return helpers.lowercase.apply(this, arguments); +}; + +/** + * Truncates a string to the specified `length`, and appends + * it with an elipsis, `…`. + * + * ```handlebars + * {{ellipsis (sanitize "foo bar baz"), 7}} + * + * {{ellipsis "foo bar baz", 7}} + * + * ``` + * @param {String} `str` + * @param {Number} `length` The desired length of the returned string. + * @return {String} The truncated string. + * @api public + */ + +helpers.ellipsis = function(str, limit) { + if (util.isString(str)) { + if (str.length <= limit) { + return str; + } + return helpers.truncate(str, limit) + '…'; + } +}; + +/** + * Replace spaces in a string with hyphens. + * + * ```handlebars + * {{hyphenate "foo bar baz qux"}} + * + * ``` + * @param {String} `str` + * @return {String} + * @api public + */ + +helpers.hyphenate = function(str) { + if (!util.isString(str)) return ''; + return str.split(' ').join('-'); +}; + +/** + * Return true if `value` is a string. + * + * ```handlebars + * {{isString "foo"}} + * + * ``` + * @param {String} `value` + * @return {Boolean} + * @api public + */ + +helpers.isString = function(value) { + return typeof value === 'string'; +}; + +/** + * Lowercase all characters in the given string. + * + * ```handlebars + * {{lowercase "Foo BAR baZ"}} + * + * ``` + * @param {String} `str` + * @return {String} + * @api public + */ + +helpers.lowercase = function(str) { + if (util.isObject(str) && str.fn) { + return str.fn(this).toLowerCase(); + } + if (!util.isString(str)) return ''; + return str.toLowerCase(); +}; + +/** + * Return the number of occurrences of `substring` within the + * given `string`. + * + * ```handlebars + * {{occurrences "foo bar foo bar baz" "foo"}} + * + * ``` + * @param {String} `str` + * @param {String} `substring` + * @return {Number} Number of occurrences + * @api public + */ + +helpers.occurrences = function(str, substring) { + if (!util.isString(str)) return ''; + var len = substring.length; + var pos = 0; + var n = 0; + + while ((pos = str.indexOf(substring, pos)) > -1) { + n++; + pos += len; + } + return n; +}; + +/** + * PascalCase the characters in `string`. + * + * ```handlebars + * {{pascalcase "foo bar baz"}} + * + * ``` + * @param {String} `string` + * @return {String} + * @api public + */ + +helpers.pascalcase = function(str) { + if (!util.isString(str)) return ''; + str = utils.changecase(str, function(ch) { + return ch.toUpperCase(); + }); + return str.charAt(0).toUpperCase() + str.slice(1); +}; + +/** + * path/case the characters in `string`. + * + * ```handlebars + * {{pathcase "a-b-c d_e"}} + * + * ``` + * @param {String} `string` + * @return {String} + * @api public + */ + +helpers.pathcase = function(str) { + if (!util.isString(str)) return ''; + return utils.changecase(str, function(ch) { + return '/' + ch; + }); +}; + +/** + * Replace spaces in the given string with pluses. + * + * ```handlebars + * {{plusify "foo bar baz"}} + * + * ``` + * @param {String} `str` The input string + * @return {String} Input string with spaces replaced by plus signs + * @source Stephen Way + * @api public + */ + +helpers.plusify = function(str, ch) { + if (!util.isString(str)) return ''; + if (!util.isString(ch)) ch = ' '; + return str.split(ch).join('+'); +}; + +/** + * Prepends the given `string` with the specified `prefix`. + * + * ```handlebars + * + * {{prepend val "foo-"}} + * + * ``` + * @param {String} `str` + * @param {String} `prefix` + * @return {String} + * @api public + */ + +helpers.prepend = function(str, prefix) { + return typeof str === 'string' && typeof prefix === 'string' + ? (prefix + str) + : str; +}; + +/** + * Render a block without processing mustache templates inside the block. + * + * ```handlebars + * {{{{#raw}}}} + * {{foo}} + * {{{{/raw}}}} + * + * ``` + * + * @param {Object} `options` + * @return {String} + * @block + * @api public + */ + +helpers.raw = function(options) { + var str = options.fn(); + var opts = util.options(this, options); + if (opts.escape !== false) { + var idx = 0; + while (((idx = str.indexOf('{{', idx)) !== -1)) { + if (str[idx - 1] !== '\\') { + str = str.slice(0, idx) + '\\' + str.slice(idx); + } + idx += 3; + } + } + return str; +}; + +/** + * Remove all occurrences of `substring` from the given `str`. + * + * ```handlebars + * {{remove "a b a b a b" "a "}} + * + * ``` + * @param {String} `str` + * @param {String} `substring` + * @return {String} + * @api public + */ + +helpers.remove = function(str, ch) { + if (!util.isString(str)) return ''; + if (!util.isString(ch)) return str; + return str.split(ch).join(''); +}; + +/** + * Remove the first occurrence of `substring` from the given `str`. + * + * ```handlebars + * {{remove "a b a b a b" "a"}} + * + * ``` + * @param {String} `str` + * @param {String} `substring` + * @return {String} + * @api public + */ + +helpers.removeFirst = function(str, ch) { + if (!util.isString(str)) return ''; + if (!util.isString(ch)) return str; + return str.replace(ch, ''); +}; + +/** + * Replace all occurrences of substring `a` with substring `b`. + * + * ```handlebars + * {{replace "a b a b a b" "a" "z"}} + * + * ``` + * @param {String} `str` + * @param {String} `a` + * @param {String} `b` + * @return {String} + * @api public + */ + +helpers.replace = function(str, a, b) { + if (!util.isString(str)) return ''; + if (!util.isString(a)) return str; + if (!util.isString(b)) b = ''; + return str.split(a).join(b); +}; + +/** + * Replace the first occurrence of substring `a` with substring `b`. + * + * ```handlebars + * {{replace "a b a b a b" "a" "z"}} + * + * ``` + * @param {String} `str` + * @param {String} `a` + * @param {String} `b` + * @return {String} + * @api public + */ + +helpers.replaceFirst = function(str, a, b) { + if (!util.isString(str)) return ''; + if (!util.isString(a)) return str; + if (!util.isString(b)) b = ''; + return str.replace(a, b); +}; + +/** + * Reverse a string. + * + * ```handlebars + * {{reverse "abcde"}} + * + * ``` + * @param {String} `str` + * @return {String} + * @api public + */ + +helpers.reverse = function(str) { + if (!util.isString(str)) return ''; + return str.split('').reverse().join(''); +}; + +/** + * Sentence case the given string + * + * ```handlebars + * {{sentence "hello world. goodbye world."}} + * + * ``` + * @param {String} `str` + * @return {String} + * @api public + */ + +helpers.sentence = function(str) { + if (!util.isString(str)) return ''; + return str.replace(/((?:\S[^\.\?\!]*)[\.\?\!]*)/g, function(txt) { + return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); + }); +}; + +/** + * snake_case the characters in the given `string`. + * + * ```handlebars + * {{snakecase "a-b-c d_e"}} + * + * ``` + * @param {String} `string` + * @return {String} + * @api public + */ + +helpers.snakecase = function(str) { + if (!util.isString(str)) return ''; + return utils.changecase(str, function(ch) { + return '_' + ch; + }); +}; + +/** + * Split `string` by the given `character`. + * + * ```handlebars + * {{split "a,b,c" ","}} + * + * ``` + * @param {String} `string` The string to split. + * @return {String} `character` Default is an empty string. + * @api public + */ + +helpers.split = function(str, ch) { + if (!util.isString(str)) return ''; + if (!util.isString(ch)) ch = ','; + return str.split(ch); +}; + +/** + * Tests whether a string begins with the given prefix. + * + * ```handlebars + * {{#startsWith "Goodbye" "Hello, world!"}} + * Whoops + * {{else}} + * Bro, do you even hello world? + * {{/startsWith}} + * ``` + * @contributor Dan Fox + * @param {String} `prefix` + * @param {String} `testString` + * @param {String} `options` + * @return {String} + * @block + * @api public + */ + +helpers.startsWith = function(prefix, str, options) { + var args = [].slice.call(arguments); + options = args.pop(); + if (util.isString(str) && str.indexOf(prefix) === 0) { + return options.fn(this); + } + if (typeof options.inverse === 'function') { + return options.inverse(this); + } + return ''; +}; + +/** + * Title case the given string. + * + * ```handlebars + * {{titleize "this is title case"}} + * + * ``` + * @param {String} `str` + * @return {String} + * @api public + */ + +helpers.titleize = function(str) { + if (!util.isString(str)) return ''; + var title = str.replace(/[- _]+/g, ' '); + var words = title.split(' '); + var len = words.length; + var res = []; + var i = 0; + while (len--) { + var word = words[i++]; + res.push(exports.capitalize(word)); + } + return res.join(' '); +}; + +/** + * Removes extraneous whitespace from the beginning and end + * of a string. + * + * ```handlebars + * {{trim " ABC "}} + * + * ``` + * @param {String} `string` The string to trim. + * @return {String} + * @api public + */ + +helpers.trim = function(str) { + return typeof str === 'string' ? str.trim() : ''; +}; + +/** + * Removes extraneous whitespace from the beginning of a string. + * + * ```handlebars + * {{trim " ABC "}} + * + * ``` + * @param {String} `string` The string to trim. + * @return {String} + * @api public + */ + +helpers.trimLeft = function(str) { + if (util.isString(str)) { + return str.replace(/^\s+/, ''); + } +}; + +/** + * Removes extraneous whitespace from the end of a string. + * + * ```handlebars + * {{trimRight " ABC "}} + * + * ``` + * @param {String} `string` The string to trim. + * @return {String} + * @api public + */ + +helpers.trimRight = function(str) { + if (util.isString(str)) { + return str.replace(/\s+$/, ''); + } +}; + +/** + * Truncate a string to the specified `length`. Also see [ellipsis](#ellipsis). + * + * ```handlebars + * truncate("foo bar baz", 7); + * + * truncate(sanitize("foo bar baz", 7)); + * + * ``` + * @param {String} `str` + * @param {Number} `limit` The desired length of the returned string. + * @param {String} `suffix` Optionally supply a string to use as a suffix to + * denote when the string has been truncated. Otherwise an ellipsis (`…`) will be used. + * @return {String} The truncated string. + * @api public + */ + +helpers.truncate = function(str, limit, suffix) { + if (util.isString(str)) { + if (typeof suffix !== 'string') { + suffix = ''; + } + if (str.length > limit) { + return str.slice(0, limit - suffix.length) + suffix; + } + return str; + } +}; + +/** + * Truncate a string to have the specified number of words. + * Also see [truncate](#truncate). + * + * ```handlebars + * truncateWords("foo bar baz", 1); + * + * truncateWords("foo bar baz", 2); + * + * truncateWords("foo bar baz", 3); + * + * ``` + * @param {String} `str` + * @param {Number} `limit` The desired length of the returned string. + * @param {String} `suffix` Optionally supply a string to use as a suffix to + * denote when the string has been truncated. + * @return {String} The truncated string. + * @api public + */ + +helpers.truncateWords = function(str, count, suffix) { + if (util.isString(str) && isNumber(count)) { + if (typeof suffix !== 'string') { + suffix = '…'; + } + + var num = Number(count); + var arr = str.split(/[ \t]/); + if (num > arr.length) { + arr = arr.slice(0, num); + } + + var val = arr.join(' ').trim(); + return val + suffix; + } +}; + +/** + * Uppercase all of the characters in the given string. Alias for [uppercase](#uppercase). + * + * ```handlebars + * {{upcase "aBcDeF"}} + * + * ``` + * @param {String} `string` + * @return {String} + * @alias uppercase + * @api public + */ + +helpers.upcase = function() { + return helpers.uppercase.apply(this, arguments); +}; + +/** + * Uppercase all of the characters in the given string. If used as a + * block helper it will uppercase the entire block. This helper + * does not support inverse blocks. + * + * ```handlebars + * {{uppercase "aBcDeF"}} + * + * ``` + * @related capitalize capitalizeAll + * @param {String} `str` The string to uppercase + * @param {Object} `options` Handlebars options object + * @return {String} + * @block + * @api public + */ + +helpers.uppercase = function(str) { + if (util.isObject(str) && str.fn) { + return str.fn(this).toUpperCase(); + } + if (!util.isString(str)) return ''; + return str.toUpperCase(); +}; diff --git a/lib/handlebars-helpers/utils.js b/lib/handlebars-helpers/utils.js new file mode 100644 index 00000000..33cc79b5 --- /dev/null +++ b/lib/handlebars-helpers/utils.js @@ -0,0 +1,50 @@ +const util = require('handlebars-utils'); + +let utils = {} + +utils.changecase = function(str, fn) { + if (!util.isString(str)) return ''; + if (str.length === 1) { + return str.toLowerCase(); + } + + str = utils.chop(str).toLowerCase(); + if (typeof fn !== 'function') { + fn = utils.identity; + } + + var re = /[-_.\W\s]+(\w|$)/g; + return str.replace(re, function(_, ch) { + return fn(ch); + }); +}; + +/** + * Generate a random number + * + * @param {Number} `min` + * @param {Number} `max` + * @return {Number} + * @api public + */ + +utils.random = function(min, max) { + return min + Math.floor(Math.random() * (max - min + 1)); +}; + +utils = { + ...utils, + ...require('is-number') +}; + +utils.chop = function(str) { + if (!util.isString(str)) return ''; + var re = /^[-_.\W\s]+|[-_.\W\s]+$/g; + return str.trim().replace(re, ''); +}; + +/** + * Expose `utils` + */ + +module.exports = utils; diff --git a/lib/handlers/prom_series.js b/lib/handlers/prom_series.js index d6862b7d..c3abd3e8 100644 --- a/lib/handlers/prom_series.js +++ b/lib/handlers/prom_series.js @@ -1,6 +1,5 @@ const { scanSeries } = require('../db/clickhouse') const { CORS } = require('../../common') -const { isArray } = require('handlebars-helpers/lib/array') const { QrynError } = require('./errors') const {series} = require('../../promql/index') @@ -14,7 +13,7 @@ async function handler (req, res) { if (query.includes('node_info')) { return res.send({ status: 'success', data: [] }) } - if (!isArray(query)) { + if (!Array.isArray(query)) { query = [query] } const startMs = req.query.start ? parseInt(req.query.start) * 1000 : Date.now() - 7 * 24 * 3600 * 1000 diff --git a/package.json b/package.json index da3106a9..f80a9070 100644 --- a/package.json +++ b/package.json @@ -54,8 +54,8 @@ "fastify-plugin": "^4.5.1", "glob": "^7.1.2", "handlebars": "^4.7.7", - "handlebars-helpers": "^0.10.0", - "micromatch": "^4.0.8", + "handlebars-utils": "^1.0.6", + "is-number": "^7.0.0", "http-errors": "^2.0.0", "json-stable-stringify": "^1.0.1", "jsonic": "^1.0.1", diff --git a/parser/registry/line_format/index.js b/parser/registry/line_format/index.js index a99279c3..271c18f4 100644 --- a/parser/registry/line_format/index.js +++ b/parser/registry/line_format/index.js @@ -3,7 +3,7 @@ const { addStream, isEOF } = require('../common') const { LineFmtOption } = require('../../../common') const { compile } = require('./go_native_fmt') const logger = require('../../../lib/logger') -require('handlebars-helpers')(['math', 'string'], { +require('../../../lib/handlebars-helpers')(['math', 'string'], { handlebars: hb }) diff --git a/test/e2e b/test/e2e index 929315ed..b09c5c87 160000 --- a/test/e2e +++ b/test/e2e @@ -1 +1 @@ -Subproject commit 929315ed5dc45d59055a5a240015f7b7c22f5504 +Subproject commit b09c5c877d6416505c390d634eb9d19a80b7e163