Просмотр исходного кода

chore: adds support Node.js 12

e22m4u 1 год назад
Родитель
Сommit
5f7ba39c85
4 измененных файлов с 144 добавлено и 1 удалено
  1. 1 0
      .husky/pre-commit
  2. 10 0
      build-cjs.js
  3. 122 0
      dist/cjs/index.js
  4. 11 1
      package.json

+ 1 - 0
.husky/pre-commit

@@ -1,5 +1,6 @@
 npm run lint:fix
 npm run format
 npm run test
+npm run build:cjs
 
 git add -A

+ 10 - 0
build-cjs.js

@@ -0,0 +1,10 @@
+import * as esbuild from 'esbuild';
+
+await esbuild.build({
+  entryPoints: ['src/index.js'],
+  outfile: 'dist/cjs/index.js',
+  format: 'cjs',
+  platform: 'node',
+  target: ['node12'],
+  bundle: true,
+});

+ 122 - 0
dist/cjs/index.js

@@ -0,0 +1,122 @@
+var __defProp = Object.defineProperty;
+var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
+var __getOwnPropNames = Object.getOwnPropertyNames;
+var __hasOwnProp = Object.prototype.hasOwnProperty;
+var __export = (target, all) => {
+  for (var name in all)
+    __defProp(target, name, { get: all[name], enumerable: true });
+};
+var __copyProps = (to, from, except, desc) => {
+  if (from && typeof from === "object" || typeof from === "function") {
+    for (let key of __getOwnPropNames(from))
+      if (!__hasOwnProp.call(to, key) && key !== except)
+        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
+  }
+  return to;
+};
+var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
+
+// src/index.js
+var src_exports = {};
+__export(src_exports, {
+  Errorf: () => Errorf,
+  format: () => format
+});
+module.exports = __toCommonJS(src_exports);
+
+// src/utils/is-class.js
+function isClass(value) {
+  if (!value) return false;
+  return typeof value === "function" && /^class\s/.test(Function.prototype.toString.call(value));
+}
+
+// src/value-to-string.js
+var BASE_CTOR_NAMES = [
+  "String",
+  "Number",
+  "Boolean",
+  "Object",
+  "Array",
+  "Function",
+  "Symbol",
+  "Map",
+  "Set",
+  "Date"
+];
+function valueToString(input) {
+  if (input == null) return String(input);
+  if (typeof input === "string") return `"${input}"`;
+  if (typeof input === "number" || typeof input === "boolean")
+    return String(input);
+  if (isClass(input)) return input.name ? input.name : "Class";
+  if (input.constructor && input.constructor.name)
+    return BASE_CTOR_NAMES.includes(input.constructor.name) ? input.constructor.name : `${input.constructor.name} (instance)`;
+  if (typeof input === "object" && input.constructor == null) return "Object";
+  return String(input);
+}
+
+// src/array-to-list.js
+var SEPARATOR = ", ";
+function arrayToList(input) {
+  if (Array.isArray(input) && input.length)
+    return input.map(valueToString).join(SEPARATOR);
+  return valueToString(input);
+}
+
+// src/format.js
+function format(pattern) {
+  if (pattern instanceof Date) {
+    pattern = pattern.toISOString();
+  } else if (typeof pattern !== "string") {
+    pattern = String(pattern);
+  }
+  const re = /(%?)(%([sdjvl]))/g;
+  const args = Array.prototype.slice.call(arguments, 1);
+  if (args.length) {
+    pattern = pattern.replace(re, function(match, escaped, ptn, flag) {
+      let arg = args.shift();
+      switch (flag) {
+        case "s":
+          arg = String(arg);
+          break;
+        case "d":
+          arg = Number(arg);
+          break;
+        case "j":
+          arg = JSON.stringify(arg);
+          break;
+        case "v":
+          arg = valueToString(arg);
+          break;
+        case "l":
+          arg = arrayToList(arg);
+          break;
+      }
+      if (!escaped) return arg;
+      args.unshift(arg);
+      return match;
+    });
+  }
+  if (args.length) pattern += " " + args.join(" ");
+  pattern = pattern.replace(/%{2}/g, "%");
+  return "" + pattern;
+}
+
+// src/errorf.js
+var Errorf = class extends Error {
+  /**
+   * Constructor.
+   *
+   * @param {string|undefined} pattern
+   * @param {any} args
+   */
+  constructor(pattern = void 0, ...args) {
+    const message = pattern != null ? format(pattern, ...args) : void 0;
+    super(message);
+  }
+};
+// Annotate the CommonJS export names for ESM import in node:
+0 && (module.exports = {
+  Errorf,
+  format
+});

+ 11 - 1
package.json

@@ -3,13 +3,22 @@
   "version": "0.1.1",
   "description": "The string interpolation utility for JavaScript",
   "type": "module",
-  "main": "src/index.js",
+  "module": "./src/index.js",
+  "main": "./dist/cjs/index.js",
+  "exports": {
+    "import": "./src/index.js",
+    "require": "./dist/cjs/index.js"
+  },
+  "engines": {
+    "node": ">=12"
+  },
   "scripts": {
     "lint": "tsc && eslint .",
     "lint:fix": "tsc && eslint . --fix",
     "format": "prettier --write \"./src/**/*.js\"",
     "test": "npm run lint && c8 --reporter=text-summary mocha",
     "test:coverage": "npm run lint && c8 --reporter=text mocha",
+    "build:cjs": "node build-cjs.js",
     "prepare": "husky"
   },
   "repository": {
@@ -30,6 +39,7 @@
     "@eslint/js": "~9.12.0",
     "c8": "~10.1.2",
     "chai": "~5.1.1",
+    "esbuild": "~0.24.0",
     "eslint": "~9.12.0",
     "eslint-config-prettier": "~9.1.0",
     "eslint-plugin-chai-expect": "~3.1.0",