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

refactor: renames DataProjectionService to DataProjector

e22m4u 1 месяц назад
Родитель
Сommit
f5341ddaa4
5 измененных файлов с 67 добавлено и 71 удалено
  1. 14 14
      README.md
  2. 9 9
      dist/cjs/index.cjs
  3. 4 4
      src/data-projector.js
  4. 39 43
      src/data-projector.spec.js
  5. 1 1
      src/index.js

+ 14 - 14
README.md

@@ -234,11 +234,11 @@ console.log(outputData);
 Использование сокращенных методов для работы с областями.
 
 ```js
-import {DataProjectionService} from '@e22m4u/js-data-projection';
+import {DataProjector} from '@e22m4u/js-data-projection';
 
-const dps = new DataProjectionService();
+const dps = new DataProjector();
 
-dps.defineProjection({
+dps.defineSchema({
   name: 'user',
   schema: {
     username: true,
@@ -343,7 +343,7 @@ import {projectData} from '@e22m4u/js-data-projection';
 
 // объект будет передан в параметры фабрики
 const logger = {
-  log: (message) => console.log(message);
+  log: (message) => console.log(message),
 };
 
 // фабрика использует logger
@@ -371,11 +371,11 @@ console.log(result);
 
 ```js
 import {ServiceContainer} from '@e22m4u/js-service';
-import {DataProjectionService} from '@e22m4u/js-data-projection';
+import {DataProjector} from '@e22m4u/js-data-projection';
 
 // сервис-контейнер доступен только
-// при использовании DataProjectionService
-const dps = new DataProjectionService();
+// при использовании DataProjector
+const dps = new DataProjector();
 
 // по умолчанию сервис-контейнер передается
 // первым аргументом фабрики
@@ -401,12 +401,12 @@ console.log(result);
 Регистрация и применение именованной схемы.
 
 ```js
-import {DataProjectionService} from '@e22m4u/js-data-projection';
+import {DataProjector} from '@e22m4u/js-data-projection';
 
-const dps = new DataProjectionService();
+const dps = new DataProjector();
 
 // регистрация схемы
-dps.defineProjection({
+dps.defineSchema({
   name: 'user', // <= имя схемы
   schema: {
     id: true,
@@ -433,12 +433,12 @@ console.log(result);
 Использование вложенных именованных схем.
 
 ```js
-import {DataProjectionService} from '@e22m4u/js-data-projection';
+import {DataProjector} from '@e22m4u/js-data-projection';
 
-const dps = new DataProjectionService();
+const dps = new DataProjector();
 
 // регистрация схемы "address"
-dps.defineProjection({
+dps.defineSchema({
   name: 'address',
   schema: {
     city: true,
@@ -447,7 +447,7 @@ dps.defineProjection({
 });
 
 // регистрация схемы "user"
-dps.defineProjection({
+dps.defineSchema({
   name: 'user',
   schema: {
     name: true,

+ 9 - 9
dist/cjs/index.cjs

@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
 // src/index.js
 var index_exports = {};
 __export(index_exports, {
-  DataProjectionService: () => DataProjectionService,
+  DataProjector: () => DataProjector,
   ProjectionSchemaRegistry: () => ProjectionSchemaRegistry,
   projectData: () => projectData,
   validateProjectionSchema: () => validateProjectionSchema,
@@ -230,7 +230,7 @@ function _shouldSelect(propOptions, strict, scope) {
 }
 __name(_shouldSelect, "_shouldSelect");
 
-// src/data-projection-service.js
+// src/data-projector.js
 var import_js_service2 = require("@e22m4u/js-service");
 
 // src/projection-schema-registry.js
@@ -330,15 +330,15 @@ var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_j
 __name(_ProjectionSchemaRegistry, "ProjectionSchemaRegistry");
 var ProjectionSchemaRegistry = _ProjectionSchemaRegistry;
 
-// src/data-projection-service.js
-var _DataProjectionService = class _DataProjectionService extends import_js_service2.Service {
+// src/data-projector.js
+var _DataProjector = class _DataProjector extends import_js_service2.Service {
   /**
-   * Define projection.
+   * Define schema.
    *
    * @param {object} schemaDef
    * @returns {this}
    */
-  defineProjection(schemaDef) {
+  defineSchema(schemaDef) {
     this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef);
     return this;
   }
@@ -380,11 +380,11 @@ var _DataProjectionService = class _DataProjectionService extends import_js_serv
     return this.projectData(schema, data, { ...options, scope: "output" });
   }
 };
-__name(_DataProjectionService, "DataProjectionService");
-var DataProjectionService = _DataProjectionService;
+__name(_DataProjector, "DataProjector");
+var DataProjector = _DataProjector;
 // Annotate the CommonJS export names for ESM import in node:
 0 && (module.exports = {
-  DataProjectionService,
+  DataProjector,
   ProjectionSchemaRegistry,
   projectData,
   validateProjectionSchema,

+ 4 - 4
src/data-projection-service.js → src/data-projector.js

@@ -3,16 +3,16 @@ import {projectData} from './project-data.js';
 import {ProjectionSchemaRegistry} from './projection-schema-registry.js';
 
 /**
- * Data projection service.
+ * Data projector.
  */
-export class DataProjectionService extends Service {
+export class DataProjector extends Service {
   /**
-   * Define projection.
+   * Define schema.
    *
    * @param {object} schemaDef
    * @returns {this}
    */
-  defineProjection(schemaDef) {
+  defineSchema(schemaDef) {
     this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef);
     return this;
   }

+ 39 - 43
src/data-projection-service.spec.js → src/data-projector.spec.js

@@ -1,12 +1,12 @@
 import {expect} from 'chai';
-import {DataProjectionService} from './data-projection-service.js';
+import {DataProjector} from './data-projector.js';
 import {ProjectionSchemaRegistry} from './projection-schema-registry.js';
 
-describe('DataProjectionService', function () {
-  describe('defineProjection', function () {
+describe('DataProjector', function () {
+  describe('defineSchema', function () {
     it('should validate the given definition', function () {
-      const dps = new DataProjectionService();
-      const throwable = () => dps.defineProjection({});
+      const S = new DataProjector();
+      const throwable = () => S.defineSchema({});
       expect(throwable).to.throw(
         'Definition option "name" must be a non-empty String, ' +
           'but undefined was given.',
@@ -15,48 +15,48 @@ describe('DataProjectionService', function () {
 
     it('should register the given definition', function () {
       const def = {name: 'mySchema', schema: {}};
-      const dps = new DataProjectionService();
-      dps.defineProjection(def);
-      const registry = dps.getService(ProjectionSchemaRegistry);
+      const S = new DataProjector();
+      S.defineSchema(def);
+      const registry = S.getService(ProjectionSchemaRegistry);
       const res = registry.getDefinition(def.name);
       expect(res).to.be.eql(def);
     });
 
     it('should throw an error if the schema name is already registered', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       const def = {name: 'mySchema', schema: {}};
-      dps.defineProjection(def);
-      const throwable = () => dps.defineProjection(def);
+      S.defineSchema(def);
+      const throwable = () => S.defineSchema(def);
       expect(throwable).to.throw(
         'Projection schema "mySchema" is already registered.',
       );
     });
 
     it('should return the current instance', function () {
-      const dps = new DataProjectionService();
-      const res = dps.defineProjection({name: 'mySchema', schema: {}});
-      expect(res).to.be.eq(dps);
+      const S = new DataProjector();
+      const res = S.defineSchema({name: 'mySchema', schema: {}});
+      expect(res).to.be.eq(S);
     });
   });
 
   describe('projectData', function () {
     it('should project the data object by the given schema', function () {
-      const dps = new DataProjectionService();
-      const res = dps.projectData({foo: true, bar: false}, {foo: 10, bar: 20});
+      const S = new DataProjector();
+      const res = S.projectData({foo: true, bar: false}, {foo: 10, bar: 20});
       expect(res).to.be.eql({foo: 10});
     });
 
     it('should project the data object by the schema name', function () {
-      const dps = new DataProjectionService();
-      dps.defineProjection({name: 'mySchema', schema: {foo: true, bar: false}});
-      const res = dps.projectData('mySchema', {foo: 10, bar: 20, baz: 30});
+      const S = new DataProjector();
+      S.defineSchema({name: 'mySchema', schema: {foo: true, bar: false}});
+      const res = S.projectData('mySchema', {foo: 10, bar: 20, baz: 30});
       expect(res).to.be.eql({foo: 10, baz: 30});
     });
 
     it('should exclude properties without rules in the strict mode', function () {
-      const dps = new DataProjectionService();
-      dps.defineProjection({name: 'mySchema', schema: {foo: true, bar: false}});
-      const res = dps.projectData(
+      const S = new DataProjector();
+      S.defineSchema({name: 'mySchema', schema: {foo: true, bar: false}});
+      const res = S.projectData(
         'mySchema',
         {foo: 10, bar: 20, baz: 30},
         {strict: true},
@@ -65,7 +65,7 @@ describe('DataProjectionService', function () {
     });
 
     it('should allow override the "nameResolver" option', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       const schemaName = 'mySchema';
       let invoked = 0;
       const nameResolver = name => {
@@ -73,30 +73,26 @@ describe('DataProjectionService', function () {
         expect(name).to.be.eq(schemaName);
         return {foo: true, bar: false};
       };
-      const res = dps.projectData(
-        'mySchema',
-        {foo: 10, bar: 20},
-        {nameResolver},
-      );
+      const res = S.projectData('mySchema', {foo: 10, bar: 20}, {nameResolver});
       expect(res).to.be.eql({foo: 10});
       expect(invoked).to.be.eq(1);
     });
 
     it('should pass the service container to the schema factory', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       let invoked = 0;
       const factory = container => {
         invoked++;
-        expect(container).to.be.eq(dps.container);
+        expect(container).to.be.eq(S.container);
         return {foo: true, bar: false};
       };
-      const res = dps.projectData(factory, {foo: 10, bar: 20});
+      const res = S.projectData(factory, {foo: 10, bar: 20});
       expect(res).to.be.eql({foo: 10});
       expect(invoked).to.be.eq(1);
     });
 
     it('should allow override the "factoryArgs" option', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       let invoked = 0;
       const factoryArgs = [1, 2, 3];
       const factory = (...args) => {
@@ -104,7 +100,7 @@ describe('DataProjectionService', function () {
         expect(args).to.be.eql(factoryArgs);
         return {foo: true, bar: false};
       };
-      const res = dps.projectData(factory, {foo: 10, bar: 20}, {factoryArgs});
+      const res = S.projectData(factory, {foo: 10, bar: 20}, {factoryArgs});
       expect(res).to.be.eql({foo: 10});
       expect(invoked).to.be.eq(1);
     });
@@ -112,60 +108,60 @@ describe('DataProjectionService', function () {
 
   describe('projectInput', function () {
     it('should invoke the "projectData" method with the "input" scope and return its result', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       const schema = {foo: true, bar: false};
       const data = {foo: 10, bar: 20};
       const options = {extra: true};
       const result = {foo: 10};
       let invoked = 0;
-      dps.projectData = (...args) => {
+      S.projectData = (...args) => {
         expect(args).to.be.eql([schema, data, {extra: true, scope: 'input'}]);
         invoked++;
         return result;
       };
-      const res = dps.projectInput(schema, data, options);
+      const res = S.projectInput(schema, data, options);
       expect(res).to.be.eq(result);
       expect(invoked).to.be.eq(1);
     });
 
     it('should project the given object with the "input" scope', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       const schema = {
         foo: {select: false, scopes: {input: true}},
         bar: {select: true, scopes: {input: false}},
       };
       const data = {foo: 10, bar: 20};
-      const res = dps.projectInput(schema, data);
+      const res = S.projectInput(schema, data);
       expect(res).to.be.eql({foo: 10});
     });
   });
 
   describe('projectOutput', function () {
     it('should invoke the "projectData" method with the "output" scope and return its result', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       const schema = {foo: true, bar: false};
       const data = {foo: 10, bar: 20};
       const options = {extra: true};
       const result = {foo: 10};
       let invoked = 0;
-      dps.projectData = (...args) => {
+      S.projectData = (...args) => {
         expect(args).to.be.eql([schema, data, {extra: true, scope: 'output'}]);
         invoked++;
         return result;
       };
-      const res = dps.projectOutput(schema, data, options);
+      const res = S.projectOutput(schema, data, options);
       expect(res).to.be.eq(result);
       expect(invoked).to.be.eq(1);
     });
 
     it('should project the given object with the "output" scope', function () {
-      const dps = new DataProjectionService();
+      const S = new DataProjector();
       const schema = {
         foo: {select: false, scopes: {output: true}},
         bar: {select: true, scopes: {output: false}},
       };
       const data = {foo: 10, bar: 20};
-      const res = dps.projectOutput(schema, data);
+      const res = S.projectOutput(schema, data);
       expect(res).to.be.eql({foo: 10});
     });
   });

+ 1 - 1
src/index.js

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