diff options
| author | Adam Mathes <adam@adammathes.com> | 2026-02-13 21:34:48 -0800 |
|---|---|---|
| committer | Adam Mathes <adam@adammathes.com> | 2026-02-13 21:34:48 -0800 |
| commit | 76cb9c2a39d477a64824a985ade40507e3bbade1 (patch) | |
| tree | 41e997aa9c6f538d3a136af61dae9424db2005a9 /vanilla/node_modules/obug/dist/core.js | |
| parent | 819a39a21ac992b1393244a4c283bbb125208c69 (diff) | |
| download | neko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.gz neko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.bz2 neko-76cb9c2a39d477a64824a985ade40507e3bbade1.zip | |
feat(vanilla): add testing infrastructure and tests (NK-wjnczv)
Diffstat (limited to 'vanilla/node_modules/obug/dist/core.js')
| -rw-r--r-- | vanilla/node_modules/obug/dist/core.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/vanilla/node_modules/obug/dist/core.js b/vanilla/node_modules/obug/dist/core.js new file mode 100644 index 0000000..db2abcd --- /dev/null +++ b/vanilla/node_modules/obug/dist/core.js @@ -0,0 +1,120 @@ +function coerce(value) { + if (value instanceof Error) return value.stack || value.message; + return value; +} +function selectColor(colors, namespace) { + let hash = 0; + for (let i = 0; i < namespace.length; i++) { + hash = (hash << 5) - hash + namespace.charCodeAt(i); + hash |= 0; + } + return colors[Math.abs(hash) % colors.length]; +} +function matchesTemplate(search, template) { + let searchIndex = 0; + let templateIndex = 0; + let starIndex = -1; + let matchIndex = 0; + while (searchIndex < search.length) if (templateIndex < template.length && (template[templateIndex] === search[searchIndex] || template[templateIndex] === "*")) if (template[templateIndex] === "*") { + starIndex = templateIndex; + matchIndex = searchIndex; + templateIndex++; + } else { + searchIndex++; + templateIndex++; + } + else if (starIndex !== -1) { + templateIndex = starIndex + 1; + matchIndex++; + searchIndex = matchIndex; + } else return false; + while (templateIndex < template.length && template[templateIndex] === "*") templateIndex++; + return templateIndex === template.length; +} +function humanize(value) { + if (value >= 1e3) return `${(value / 1e3).toFixed(1)}s`; + return `${value}ms`; +} +let globalNamespaces = ""; +function namespaces() { + return globalNamespaces; +} +function createDebug(namespace, options) { + let prevTime; + let enableOverride; + let namespacesCache; + let enabledCache; + const debug = (...args) => { + if (!debug.enabled) return; + const curr = Date.now(); + const diff = curr - (prevTime || curr); + prevTime = curr; + args[0] = coerce(args[0]); + if (typeof args[0] !== "string") args.unshift("%O"); + let index = 0; + args[0] = args[0].replace(/%([a-z%])/gi, (match, format) => { + if (match === "%%") return "%"; + index++; + const formatter = options.formatters[format]; + if (typeof formatter === "function") { + const value = args[index]; + match = formatter.call(debug, value); + args.splice(index, 1); + index--; + } + return match; + }); + options.formatArgs.call(debug, diff, args); + debug.log(...args); + }; + debug.extend = function(namespace$1, delimiter = ":") { + return createDebug(this.namespace + delimiter + namespace$1, { + useColors: this.useColors, + color: this.color, + formatArgs: this.formatArgs, + formatters: this.formatters, + inspectOpts: this.inspectOpts, + log: this.log, + humanize: this.humanize + }); + }; + Object.assign(debug, options); + debug.namespace = namespace; + Object.defineProperty(debug, "enabled", { + enumerable: true, + configurable: false, + get: () => { + if (enableOverride != null) return enableOverride; + if (namespacesCache !== globalNamespaces) { + namespacesCache = globalNamespaces; + enabledCache = enabled(namespace); + } + return enabledCache; + }, + set: (v) => { + enableOverride = v; + } + }); + return debug; +} +let names = []; +let skips = []; +function enable(namespaces$1) { + globalNamespaces = namespaces$1; + names = []; + skips = []; + const split = globalNamespaces.trim().replace(/\s+/g, ",").split(",").filter(Boolean); + for (const ns of split) if (ns[0] === "-") skips.push(ns.slice(1)); + else names.push(ns); +} +function disable() { + const namespaces$1 = [...names, ...skips.map((namespace) => `-${namespace}`)].join(","); + enable(""); + return namespaces$1; +} +function enabled(name) { + for (const skip of skips) if (matchesTemplate(name, skip)) return false; + for (const ns of names) if (matchesTemplate(name, ns)) return true; + return false; +} +export { namespaces as a, enabled as i, disable as n, humanize as o, enable as r, selectColor as s, createDebug as t }; |
