Browse Source

refactor: rollback

e22m4u 3 days ago
parent
commit
9ad639b0d5

+ 4 - 0
.mocharc.cjs

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

+ 0 - 4
.mocharc.json

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

+ 0 - 14
README.md

@@ -28,20 +28,6 @@ JavaScript модуль для работы с проекцией данных.
 npm install @e22m4u/js-data-projector
 npm install @e22m4u/js-data-projector
 ```
 ```
 
 
-Модуль поддерживает ESM и CommonJS стандарты.
-
-*ESM*
-
-```js
-import {projectData} from '@e22m4u/js-data-projector';
-```
-
-*CommonJS*
-
-```js
-const {projectData} = require('@e22m4u/js-data-projector');
-```
-
 ## Использование
 ## Использование
 
 
 Модуль экспортирует функцию `projectData` и класс `DataProjector`.
 Модуль экспортирует функцию `projectData` и класс `DataProjector`.

+ 0 - 2
dist/cjs/index.cjs

@@ -236,8 +236,6 @@ var import_js_format3 = require("@e22m4u/js-format");
 var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service {
 var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service {
   /**
   /**
    * Schema map.
    * Schema map.
-   *
-   * @type {Map.<string, object>}
    */
    */
   _schemas = /* @__PURE__ */ new Map();
   _schemas = /* @__PURE__ */ new Map();
   /**
   /**

+ 0 - 3
eslint.config.js

@@ -2,7 +2,6 @@ import globals from 'globals';
 import eslintJs from '@eslint/js';
 import eslintJs from '@eslint/js';
 import eslintJsdocPlugin from 'eslint-plugin-jsdoc';
 import eslintJsdocPlugin from 'eslint-plugin-jsdoc';
 import eslintMochaPlugin from 'eslint-plugin-mocha';
 import eslintMochaPlugin from 'eslint-plugin-mocha';
-import eslintImportPlugin from 'eslint-plugin-import';
 import eslintPrettierConfig from 'eslint-config-prettier';
 import eslintPrettierConfig from 'eslint-config-prettier';
 import eslintChaiExpectPlugin from 'eslint-plugin-chai-expect';
 import eslintChaiExpectPlugin from 'eslint-plugin-chai-expect';
 
 
@@ -17,13 +16,11 @@ export default [{
   plugins: {
   plugins: {
     'jsdoc': eslintJsdocPlugin,
     'jsdoc': eslintJsdocPlugin,
     'mocha': eslintMochaPlugin,
     'mocha': eslintMochaPlugin,
-    'import': eslintImportPlugin,
     'chai-expect': eslintChaiExpectPlugin,
     'chai-expect': eslintChaiExpectPlugin,
   },
   },
   rules: {
   rules: {
     ...eslintJs.configs.recommended.rules,
     ...eslintJs.configs.recommended.rules,
     ...eslintPrettierConfig.rules,
     ...eslintPrettierConfig.rules,
-    ...eslintImportPlugin.flatConfigs.recommended.rules,
     ...eslintMochaPlugin.configs.recommended.rules,
     ...eslintMochaPlugin.configs.recommended.rules,
     ...eslintChaiExpectPlugin.configs['recommended-flat'].rules,
     ...eslintChaiExpectPlugin.configs['recommended-flat'].rules,
     ...eslintJsdocPlugin.configs['flat/recommended-error'].rules,
     ...eslintJsdocPlugin.configs['flat/recommended-error'].rules,

+ 0 - 7
jsconfig.json

@@ -1,7 +0,0 @@
-{
-  "compilerOptions": {
-    "target": "es2022",
-    "module": "NodeNext",
-    "moduleResolution": "NodeNext"
-  }
-}

+ 12 - 9
package.json

@@ -1,6 +1,6 @@
 {
 {
   "name": "@e22m4u/js-data-projector",
   "name": "@e22m4u/js-data-projector",
-  "version": "0.0.2",
+  "version": "0.0.1",
   "description": "JavaScript модуль для работы с проекцией данных",
   "description": "JavaScript модуль для работы с проекцией данных",
   "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
   "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
   "license": "MIT",
   "license": "MIT",
@@ -11,15 +11,17 @@
     "projection",
     "projection",
     "filtering"
     "filtering"
   ],
   ],
-  "homepage": "https://gitrepos.ru/e22m4u/js-data-projector",
+  "homepage": "https://github.com/e22m4u/js-data-projector",
   "repository": {
   "repository": {
     "type": "git",
     "type": "git",
-    "url": "git+https://gitrepos.ru/e22m4u/js-data-projector.git"
+    "url": "git+https://github.com/e22m4u/js-data-projector.git"
   },
   },
   "type": "module",
   "type": "module",
+  "types": "./src/index.d.ts",
   "module": "./src/index.js",
   "module": "./src/index.js",
   "main": "./dist/cjs/index.cjs",
   "main": "./dist/cjs/index.cjs",
   "exports": {
   "exports": {
+    "types": "./src/index.d.ts",
     "import": "./src/index.js",
     "import": "./src/index.js",
     "require": "./dist/cjs/index.cjs"
     "require": "./dist/cjs/index.cjs"
   },
   },
@@ -27,12 +29,12 @@
     "node": ">=12"
     "node": ">=12"
   },
   },
   "scripts": {
   "scripts": {
-    "lint": "eslint ./src",
-    "lint:fix": "eslint ./src --fix",
+    "lint": "tsc && eslint ./src",
+    "lint:fix": "tsc && eslint ./src --fix",
     "format": "prettier --write \"./src/**/*.js\"",
     "format": "prettier --write \"./src/**/*.js\"",
     "test": "npm run lint && c8 --reporter=text-summary mocha --bail",
     "test": "npm run lint && c8 --reporter=text-summary mocha --bail",
     "test:coverage": "npm run lint && c8 --reporter=text mocha --bail",
     "test:coverage": "npm run lint && c8 --reporter=text mocha --bail",
-    "build:cjs": "rimraf ./dist/cjs && node build-cjs.js",
+    "build:cjs": "rimraf ./dist/cjs && node --no-warnings=ExperimentalWarning build-cjs.js",
     "prepare": "husky"
     "prepare": "husky"
   },
   },
   "dependencies": {
   "dependencies": {
@@ -43,19 +45,20 @@
     "@commitlint/cli": "~20.1.0",
     "@commitlint/cli": "~20.1.0",
     "@commitlint/config-conventional": "~20.0.0",
     "@commitlint/config-conventional": "~20.0.0",
     "@eslint/js": "~9.39.1",
     "@eslint/js": "~9.39.1",
+    "@types/mocha": "~10.0.10",
     "c8": "~10.1.3",
     "c8": "~10.1.3",
     "chai": "~6.2.1",
     "chai": "~6.2.1",
     "esbuild": "~0.27.0",
     "esbuild": "~0.27.0",
     "eslint": "~9.39.1",
     "eslint": "~9.39.1",
     "eslint-config-prettier": "~10.1.8",
     "eslint-config-prettier": "~10.1.8",
     "eslint-plugin-chai-expect": "~3.1.0",
     "eslint-plugin-chai-expect": "~3.1.0",
-    "eslint-plugin-import": "~2.32.0",
     "eslint-plugin-jsdoc": "~61.4.1",
     "eslint-plugin-jsdoc": "~61.4.1",
     "eslint-plugin-mocha": "~11.2.0",
     "eslint-plugin-mocha": "~11.2.0",
     "globals": "~16.5.0",
     "globals": "~16.5.0",
     "husky": "~9.1.7",
     "husky": "~9.1.7",
     "mocha": "~11.7.5",
     "mocha": "~11.7.5",
-    "prettier": "~3.7.3",
-    "rimraf": "~6.1.2"
+    "prettier": "~3.7.2",
+    "rimraf": "~6.1.2",
+    "typescript": "~5.9.3"
   }
   }
 }
 }

+ 55 - 0
src/data-projector.d.ts

@@ -0,0 +1,55 @@
+import {Service} from '@e22m4u/js-service';
+import {ProjectDataOptions} from './project-data.js';
+import {ProjectionSchema} from './projection-schema.js';
+
+/**
+ * Data projector.
+ */
+export declare class DataProjector extends Service {
+  /**
+   * Define schema.
+   *
+   * @param name
+   * @param schema
+   */
+  defineSchema(name: string, schema: ProjectionSchema): this;
+
+  /**
+   * Project.
+   *
+   * @param schemaOrName
+   * @param data
+   * @param options
+   */
+  project<T>(
+    schemaOrName: string | ProjectionSchema,
+    data: T,
+    options?: Omit<ProjectDataOptions, 'resolver'>,
+  ): T;
+
+  /**
+   * Project with "input" scope.
+   *
+   * @param schemaOrName
+   * @param data
+   * @param options
+   */
+  projectInput<T>(
+    schemaOrName: string | ProjectionSchema,
+    data: T,
+    options?: Omit<ProjectDataOptions, 'resolver' | 'scope'>,
+  ): T;
+
+  /**
+   * Project with "output" scope.
+   *
+   * @param schemaOrName
+   * @param data
+   * @param options
+   */
+  projectOutput<T>(
+    schemaOrName: string | ProjectionSchema,
+    data: T,
+    options?: Omit<ProjectDataOptions, 'resolver' | 'scope'>,
+  ): T;
+}

+ 2 - 0
src/data-projector.spec.js

@@ -142,6 +142,7 @@ describe('DataProjector', function () {
 
 
     it('should throw an error when the resolver option is provided', function () {
     it('should throw an error when the resolver option is provided', function () {
       const S = new DataProjector();
       const S = new DataProjector();
+      // @ts-ignore
       const throwable = v => () => S.project({}, {}, {resolver: v});
       const throwable = v => () => S.project({}, {}, {resolver: v});
       const error = 'Option "resolver" is not supported for the DataProjector.';
       const error = 'Option "resolver" is not supported for the DataProjector.';
       expect(throwable('str')).to.throw(error);
       expect(throwable('str')).to.throw(error);
@@ -158,6 +159,7 @@ describe('DataProjector', function () {
 
 
     it('should validate the given schema object', function () {
     it('should validate the given schema object', function () {
       const S = new DataProjector();
       const S = new DataProjector();
+      // @ts-ignore
       const throwable = () => S.project({foo: 10}, {foo: 'bar'});
       const throwable = () => S.project({foo: 10}, {foo: 'bar'});
       expect(throwable).to.throw(
       expect(throwable).to.throw(
         'Property options must be a Boolean or an Object, but 10 was given.',
         'Property options must be a Boolean or an Object, but 10 was given.',

+ 6 - 0
src/index.d.ts

@@ -0,0 +1,6 @@
+export * from './project-data.js';
+export * from './data-projector.js';
+export * from './projection-scope.js';
+export * from './projection-schema.js';
+export * from './projection-schema-registry.js';
+export * from './validate-projection-schema.js';

+ 1 - 0
src/index.js

@@ -1,5 +1,6 @@
 export * from './project-data.js';
 export * from './project-data.js';
 export * from './data-projector.js';
 export * from './data-projector.js';
 export * from './projection-scope.js';
 export * from './projection-scope.js';
+export * from './projection-schema.js';
 export * from './projection-schema-registry.js';
 export * from './projection-schema-registry.js';
 export * from './validate-projection-schema.js';
 export * from './validate-projection-schema.js';

+ 30 - 0
src/project-data.d.ts

@@ -0,0 +1,30 @@
+import {ProjectionSchema} from './projection-schema.js';
+
+/**
+ * Projection schema resolver.
+ */
+export type ProjectionSchemaResolver = (
+  schemaName: string,
+) => ProjectionSchema;
+
+/**
+ * Project data options.
+ */
+export type ProjectDataOptions = {
+  strict?: boolean;
+  scope?: string;
+  resolver?: ProjectionSchemaResolver;
+};
+
+/**
+ * Project data.
+ *
+ * @param schemaOrName
+ * @param data
+ * @param options
+ */
+export declare function projectData<T>(
+  schemaOrName: string | ProjectionSchema,
+  data: T,
+  options?: ProjectDataOptions,
+): T;

+ 22 - 0
src/projection-schema-registry.d.ts

@@ -0,0 +1,22 @@
+import {Service} from '@e22m4u/js-service';
+import {ProjectionSchema} from './projection-schema.js';
+
+/**
+ * Projection schema registry.
+ */
+export declare class ProjectionSchemaRegistry extends Service {
+  /**
+   * Define schema.
+   *
+   * @param name
+   * @param schema
+   */
+  defineSchema(name: string, schema: ProjectionSchema): this;
+
+  /**
+   * Get schema.
+   *
+   * @param name
+   */
+  getSchema(name: string): ProjectionSchema;
+}

+ 0 - 2
src/projection-schema-registry.js

@@ -8,8 +8,6 @@ import {validateProjectionSchema} from './validate-projection-schema.js';
 export class ProjectionSchemaRegistry extends Service {
 export class ProjectionSchemaRegistry extends Service {
   /**
   /**
    * Schema map.
    * Schema map.
-   *
-   * @type {Map.<string, object>}
    */
    */
   _schemas = new Map();
   _schemas = new Map();
 
 

+ 29 - 0
src/projection-schema.d.ts

@@ -0,0 +1,29 @@
+/**
+ * Projection schema.
+ */
+export type ProjectionSchema = {
+  [property: string]: boolean | ProjectionSchemaPropertyOptions | undefined;
+}
+
+/**
+ * Projection schema property options.
+ */
+export type ProjectionSchemaPropertyOptions = {
+  select?: boolean;
+  scopes?: ProjectionSchemaScopes;
+  schema?: string | ProjectionSchema;
+}
+
+/**
+ * Projection schema scopes.
+ */
+export type ProjectionSchemaScopes = {
+  [scope: string]: boolean | ProjectionSchemaScopeOptions | undefined;
+}
+
+/**
+ * Projection schema scope options.
+ */
+export type ProjectionSchemaScopeOptions = {
+  select?: boolean;
+}

+ 1 - 0
src/projection-schema.js

@@ -0,0 +1 @@
+export {};

+ 13 - 0
src/projection-scope.d.ts

@@ -0,0 +1,13 @@
+/**
+ * Projection scope.
+ */
+export declare const ProjectionScope: {
+  INPUT: 'input',
+  OUTPUT: 'output',
+};
+
+/**
+ * Projection scope.
+ */
+export type ProjectionScope =
+  (typeof ProjectionScope)[keyof typeof ProjectionScope];

+ 12 - 0
src/validate-projection-schema.d.ts

@@ -0,0 +1,12 @@
+import {ProjectionSchema} from './projection-schema.js';
+
+/**
+ * Validate projection schema.
+ *
+ * @param schema
+ * @param shallowMode
+ */
+export declare function validateProjectionSchema(
+  schema: ProjectionSchema,
+  shallowMode?: boolean
+): void;

+ 2 - 0
src/validate-projection-schema.spec.js

@@ -176,6 +176,7 @@ describe('validateProjectionSchema', function () {
   });
   });
 
 
   it('should validate root schema in shallow mode', function () {
   it('should validate root schema in shallow mode', function () {
+    // @ts-ignore
     const throwable = () => validateProjectionSchema({foo: 10}, true);
     const throwable = () => validateProjectionSchema({foo: 10}, true);
     expect(throwable).to.throw(
     expect(throwable).to.throw(
       'Property options must be a Boolean or an Object, but 10 was given.',
       'Property options must be a Boolean or an Object, but 10 was given.',
@@ -183,6 +184,7 @@ describe('validateProjectionSchema', function () {
   });
   });
 
 
   it('should skip nested schema checking in shallow mode', function () {
   it('should skip nested schema checking in shallow mode', function () {
+    // @ts-ignore
     validateProjectionSchema({foo: {schema: {prop: 10}}}, true);
     validateProjectionSchema({foo: {schema: {prop: 10}}}, true);
   });
   });
 });
 });

+ 14 - 0
tsconfig.json

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