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

refactor: adds type definitions

e22m4u 3 дней назад
Родитель
Сommit
c4b0afc046

+ 0 - 4
.mocharc.cjs

@@ -1,4 +0,0 @@
-module.exports = {
-  extension: ['js'],
-  spec: 'src/**/*.spec.js',
-}

+ 4 - 0
.mocharc.json

@@ -0,0 +1,4 @@
+{
+  "extension": ["js"],
+  "spec": "src/**/*.spec.js"
+}

+ 8 - 7
dist/cjs/index.cjs

@@ -1,3 +1,4 @@
+"use strict";
 var __defProp = Object.defineProperty;
 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
 var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -71,17 +72,17 @@ function arrayToString(input) {
 __name(arrayToString, "arrayToString");
 
 // src/format.js
-function format(pattern) {
+function format(pattern, ...args) {
   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) {
+  const argsQueue = [...args];
+  if (argsQueue.length) {
     pattern = pattern.replace(re, function(match, escaped, ptn, flag) {
-      let arg = args.shift();
+      let arg = argsQueue.shift();
       switch (flag) {
         case "s":
           arg = String(arg);
@@ -100,11 +101,11 @@ function format(pattern) {
           break;
       }
       if (!escaped) return arg;
-      args.unshift(arg);
+      argsQueue.unshift(arg);
       return match;
     });
   }
-  if (args.length) pattern += " " + args.join(" ");
+  if (argsQueue.length) pattern += " " + argsQueue.join(" ");
   pattern = pattern.replace(/%{2}/g, "%");
   return "" + pattern;
 }
@@ -116,7 +117,7 @@ var _Errorf = class _Errorf extends Error {
    * Constructor.
    *
    * @param {string|undefined} pattern
-   * @param {any} args
+   * @param {*} args
    */
   constructor(pattern = void 0, ...args) {
     const message = pattern != null ? format(pattern, ...args) : void 0;

+ 14 - 0
eslint.config.js

@@ -1,6 +1,8 @@
 import globals from 'globals';
 import eslintJs from '@eslint/js';
+import eslintJsdocPlugin from 'eslint-plugin-jsdoc';
 import eslintMochaPlugin from 'eslint-plugin-mocha';
+import eslintImportPlugin from 'eslint-plugin-import';
 import eslintPrettierConfig from 'eslint-config-prettier';
 import eslintChaiExpectPlugin from 'eslint-plugin-chai-expect';
 
@@ -13,14 +15,26 @@ export default [{
     },
   },
   plugins: {
+    'jsdoc': eslintJsdocPlugin,
     'mocha': eslintMochaPlugin,
+    'import': eslintImportPlugin,
     'chai-expect': eslintChaiExpectPlugin,
   },
   rules: {
     ...eslintJs.configs.recommended.rules,
     ...eslintPrettierConfig.rules,
+    ...eslintImportPlugin.flatConfigs.recommended.rules,
     ...eslintMochaPlugin.configs.recommended.rules,
     ...eslintChaiExpectPlugin.configs['recommended-flat'].rules,
+    ...eslintJsdocPlugin.configs['flat/recommended-error'].rules,
+    'no-duplicate-imports': 'error',
+    'import/export': 0,
+    'jsdoc/reject-any-type': 0,
+    'jsdoc/reject-function-type': 0,
+    'jsdoc/require-param-description': 0,
+    'jsdoc/require-returns-description': 0,
+    'jsdoc/require-property-description': 0,
+    'jsdoc/tag-lines': ['error', 'any', {startLines: 1}],
   },
   files: ['src/**/*.js'],
 }];

+ 7 - 3
package.json

@@ -9,10 +9,10 @@
     "format",
     "error"
   ],
-  "homepage": "https://github.com/e22m4u/js-format",
+  "homepage": "https://gitrepos.ru/e22m4u/js-format",
   "repository": {
     "type": "git",
-    "url": "git+https://github.com/e22m4u/js-format.git"
+    "url": "git+https://gitrepos.ru/e22m4u/js-format.git"
   },
   "type": "module",
   "types": "./src/index.d.ts",
@@ -39,17 +39,21 @@
     "@commitlint/cli": "~20.1.0",
     "@commitlint/config-conventional": "~20.0.0",
     "@eslint/js": "~9.39.1",
+    "@types/chai": "~5.2.3",
+    "@types/mocha": "~10.0.10",
     "c8": "~10.1.3",
     "chai": "~6.2.1",
     "esbuild": "~0.27.0",
     "eslint": "~9.39.1",
     "eslint-config-prettier": "~10.1.8",
     "eslint-plugin-chai-expect": "~3.1.0",
+    "eslint-plugin-import": "~2.32.0",
+    "eslint-plugin-jsdoc": "~61.4.1",
     "eslint-plugin-mocha": "~11.2.0",
     "globals": "~16.5.0",
     "husky": "~9.1.7",
     "mocha": "~11.7.5",
-    "prettier": "~3.7.2",
+    "prettier": "~3.7.4",
     "rimraf": "~6.1.2",
     "typescript": "~5.9.3"
   }

+ 2 - 2
src/array-to-string.js

@@ -10,8 +10,8 @@ const SEPARATOR = ', ';
 /**
  * Array to string.
  *
- * @param {any} input
- * @return {string}
+ * @param {*} input
+ * @returns {string}
  */
 export function arrayToString(input) {
   if (Array.isArray(input) && input.length)

+ 1 - 0
src/array-to-string.spec.js

@@ -1,3 +1,4 @@
+/* eslint-disable jsdoc/require-jsdoc */
 import {expect} from 'chai';
 import {arrayToString} from './array-to-string.js';
 

+ 1 - 1
src/errorf.js

@@ -8,7 +8,7 @@ export class Errorf extends Error {
    * Constructor.
    *
    * @param {string|undefined} pattern
-   * @param {any} args
+   * @param {*} args
    */
   constructor(pattern = undefined, ...args) {
     const message = pattern != null ? format(pattern, ...args) : undefined;

+ 10 - 9
src/format.js

@@ -13,20 +13,21 @@ import {valueToString} from './value-to-string.js';
  * v - value (valueToString.js)
  * l - list (arrayToString.js)
  *
- * @param {string} pattern
- * @return {string}
+ * @param {*} pattern
+ * @param {...*} args
+ * @returns {string}
  */
-export function format(pattern) {
+export function format(pattern, ...args) {
   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) {
+  const argsQueue = [...args];
+  if (argsQueue.length) {
     pattern = pattern.replace(re, function (match, escaped, ptn, flag) {
-      let arg = args.shift();
+      let arg = argsQueue.shift();
       switch (flag) {
         case 's':
           arg = String(arg);
@@ -45,12 +46,12 @@ export function format(pattern) {
           break;
       }
       if (!escaped) return arg;
-      args.unshift(arg);
+      argsQueue.unshift(arg);
       return match;
     });
   }
-  // arguments remain after formatting
-  if (args.length) pattern += ' ' + args.join(' ');
+  // has arguments after interpolation
+  if (argsQueue.length) pattern += ' ' + argsQueue.join(' ');
   // update escaped %% values
   pattern = pattern.replace(/%{2}/g, '%');
   return '' + pattern;

+ 1 - 0
src/format.spec.js

@@ -1,3 +1,4 @@
+/* eslint-disable jsdoc/require-jsdoc */
 import {expect} from 'chai';
 import {format} from './format.js';
 

+ 2 - 2
src/value-to-string.js

@@ -16,8 +16,8 @@ const BASE_CTOR_NAMES = [
 /**
  * Value to string.
  *
- * @param {any} input
- * @return {string}
+ * @param {*} input
+ * @returns {string}
  */
 export function valueToString(input) {
   if (input == null) return String(input);

+ 1 - 0
src/value-to-string.spec.js

@@ -1,3 +1,4 @@
+/* eslint-disable jsdoc/require-jsdoc */
 import {expect} from 'chai';
 import {valueToString} from './value-to-string.js';
 

+ 9 - 4
tsconfig.json

@@ -1,9 +1,14 @@
 {
   "compilerOptions": {
-    "rootDir": "src",
-    "noEmit": true,
+    "strict": true,
     "target": "es2022",
     "module": "NodeNext",
-    "moduleResolution": "NodeNext"
-  }
+    "moduleResolution": "NodeNext",
+    "noEmit": true,
+    "allowJs": true
+  },
+  "include": [
+    "./src/**/*.ts",
+    "./src/**/*.js"
+  ]
 }