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

refactor: renames method projectData to project

e22m4u 3 недель назад
Родитель
Сommit
da2dccde63
5 измененных файлов с 43 добавлено и 43 удалено
  1. 22 22
      README.md
  2. 4 4
      dist/cjs/index.cjs
  3. 2 2
      src/data-projector.d.ts
  4. 4 4
      src/data-projector.js
  5. 11 11
      src/data-projector.spec.js

+ 22 - 22
README.md

@@ -236,9 +236,9 @@ console.log(outputData);
 ```js
 ```js
 import {DataProjector} from '@e22m4u/js-data-projection';
 import {DataProjector} from '@e22m4u/js-data-projection';
 
 
-const dps = new DataProjector();
+const projector = new DataProjector();
 
 
-dps.defineSchema({
+projector.defineSchema({
   name: 'user',
   name: 'user',
   schema: {
   schema: {
     username: true,
     username: true,
@@ -256,16 +256,16 @@ const data = {
   password: 'secret123',
   password: 'secret123',
 }
 }
 
 
-// аналог dps.projectData(data, 'user', {scope: 'input'})
-const input = dps.projectInput(data, 'user');
+// аналог projector.project(data, 'user', {scope: 'input'})
+const input = projector.projectInput(data, 'user');
 console.log(input);
 console.log(input);
 // {
 // {
 //   username: 'john_doe',
 //   username: 'john_doe',
 //   password: 'secret123'
 //   password: 'secret123'
 // }
 // }
 
 
-// аналог dps.projectData(data, 'user', {scope: 'output'})
-const output = dps.projectOutput(data, 'user');
+// аналог projector.project(data, 'user', {scope: 'output'})
+const output = projector.projectOutput(data, 'user');
 console.log(output);
 console.log(output);
 // {
 // {
 //   username: 'john_doe'
 //   username: 'john_doe'
@@ -377,7 +377,7 @@ import {DataProjector} from '@e22m4u/js-data-projection';
 
 
 // сервис-контейнер доступен только
 // сервис-контейнер доступен только
 // при использовании DataProjector
 // при использовании DataProjector
-const dps = new DataProjector();
+const projector = new DataProjector();
 
 
 // сервис-контейнер передается
 // сервис-контейнер передается
 // первым аргументом фабрики
 // первым аргументом фабрики
@@ -391,7 +391,7 @@ const data = {
   secret: 'john123',
   secret: 'john123',
 };
 };
 
 
-const result = dps.projectData(data, getSchema);
+const result = projector.project(data, getSchema);
 console.log(result);
 console.log(result);
 // {
 // {
 //   name: 'John',
 //   name: 'John',
@@ -405,10 +405,10 @@ console.log(result);
 ```js
 ```js
 import {DataProjector} from '@e22m4u/js-data-projection';
 import {DataProjector} from '@e22m4u/js-data-projection';
 
 
-const dps = new DataProjector();
+const projector = new DataProjector();
 
 
 // регистрация схемы
 // регистрация схемы
-dps.defineSchema({
+projector.defineSchema({
   name: 'user', // <= имя схемы
   name: 'user', // <= имя схемы
   schema: {
   schema: {
     id: true,
     id: true,
@@ -424,7 +424,7 @@ const data = {
 };
 };
 
 
 // проекция данных по зарегистрированному имени
 // проекция данных по зарегистрированному имени
-const result = dps.projectData(data, 'user');
+const result = projector.project(data, 'user');
 console.log(result);
 console.log(result);
 // {
 // {
 //   id: 10,
 //   id: 10,
@@ -437,10 +437,10 @@ console.log(result);
 ```js
 ```js
 import {DataProjector} from '@e22m4u/js-data-projection';
 import {DataProjector} from '@e22m4u/js-data-projection';
 
 
-const dps = new DataProjector();
+const projector = new DataProjector();
 
 
 // регистрация схемы "address"
 // регистрация схемы "address"
-dps.defineSchema({
+projector.defineSchema({
   name: 'address',
   name: 'address',
   schema: {
   schema: {
     city: true,
     city: true,
@@ -449,7 +449,7 @@ dps.defineSchema({
 });
 });
 
 
 // регистрация схемы "user"
 // регистрация схемы "user"
-dps.defineSchema({
+projector.defineSchema({
   name: 'user',
   name: 'user',
   schema: {
   schema: {
     name: true,
     name: true,
@@ -468,7 +468,7 @@ const data = {
   },
   },
 };
 };
 
 
-const result = dps.projectData(data, 'user');
+const result = projector.project(data, 'user');
 console.log(result);
 console.log(result);
 // {
 // {
 //   name: 'Fedor',
 //   name: 'Fedor',
@@ -483,10 +483,10 @@ console.log(result);
 ```js
 ```js
 import {DataProjector} from '@e22m4u/js-data-projection';
 import {DataProjector} from '@e22m4u/js-data-projection';
 
 
-const dps = new DataProjector();
+const projector = new DataProjector();
 
 
 // регистрация основной схемы
 // регистрация основной схемы
-dps.defineSchema({
+projector.defineSchema({
   name: 'user',
   name: 'user',
   schema: {
   schema: {
     id: true,
     id: true,
@@ -496,7 +496,7 @@ dps.defineSchema({
 });
 });
 
 
 // регистрация псевдонима
 // регистрация псевдонима
-dps.defineSchema({
+projector.defineSchema({
   name: 'author', // имя псевдонима
   name: 'author', // имя псевдонима
   schema: 'user', // <= имя основной схемы
   schema: 'user', // <= имя основной схемы
 });
 });
@@ -508,7 +508,7 @@ const data = {
 };
 };
 
 
 // проекция данных по имени псевдонима
 // проекция данных по имени псевдонима
-const result = dps.projectData(data, 'author');
+const result = projector.project(data, 'author');
 console.log(result);
 console.log(result);
 // {
 // {
 //   id: 10,
 //   id: 10,
@@ -521,7 +521,7 @@ console.log(result);
 ```js
 ```js
 import {DataProjector} from '@e22m4u/js-data-projection';
 import {DataProjector} from '@e22m4u/js-data-projection';
 
 
-const dps = new DataProjector();
+const projector = new DataProjector();
 
 
 // создание фабрики
 // создание фабрики
 const getUserSchema = () => {
 const getUserSchema = () => {
@@ -533,7 +533,7 @@ const getUserSchema = () => {
 };
 };
 
 
 // регистрация схемы
 // регистрация схемы
-dps.defineSchema({
+projector.defineSchema({
   name: 'user',
   name: 'user',
   schema: getUserSchema, // <= фабрика вместо схемы
   schema: getUserSchema, // <= фабрика вместо схемы
 });
 });
@@ -544,7 +544,7 @@ const data = {
   email: 'fedor@example.com',
   email: 'fedor@example.com',
 };
 };
 
 
-const result = dps.projectData(data, 'user');
+const result = projector.project(data, 'user');
 console.log(result);
 console.log(result);
 // {
 // {
 //   id: 10,
 //   id: 10,

+ 4 - 4
dist/cjs/index.cjs

@@ -347,14 +347,14 @@ var _DataProjector = class _DataProjector extends import_js_service2.Service {
     return this;
     return this;
   }
   }
   /**
   /**
-   * Project data.
+   * Project.
    *
    *
    * @param {object|object[]|*} data
    * @param {object|object[]|*} data
    * @param {object|Function|string} schema
    * @param {object|Function|string} schema
    * @param {object} [options]
    * @param {object} [options]
    * @returns {*}
    * @returns {*}
    */
    */
-  projectData(data, schema, options) {
+  project(data, schema, options) {
     const registry = this.getService(ProjectionSchemaRegistry);
     const registry = this.getService(ProjectionSchemaRegistry);
     const defaultNameResolver = /* @__PURE__ */ __name((name) => registry.getSchema(name), "defaultNameResolver");
     const defaultNameResolver = /* @__PURE__ */ __name((name) => registry.getSchema(name), "defaultNameResolver");
     const nameResolver = options && options.nameResolver || defaultNameResolver;
     const nameResolver = options && options.nameResolver || defaultNameResolver;
@@ -370,7 +370,7 @@ var _DataProjector = class _DataProjector extends import_js_service2.Service {
    * @returns {*}
    * @returns {*}
    */
    */
   projectInput(data, schema, options) {
   projectInput(data, schema, options) {
-    return this.projectData(data, schema, { ...options, scope: "input" });
+    return this.project(data, schema, { ...options, scope: "input" });
   }
   }
   /**
   /**
    * Project output.
    * Project output.
@@ -381,7 +381,7 @@ var _DataProjector = class _DataProjector extends import_js_service2.Service {
    * @returns {*}
    * @returns {*}
    */
    */
   projectOutput(data, schema, options) {
   projectOutput(data, schema, options) {
-    return this.projectData(data, schema, { ...options, scope: "output" });
+    return this.project(data, schema, { ...options, scope: "output" });
   }
   }
 };
 };
 __name(_DataProjector, "DataProjector");
 __name(_DataProjector, "DataProjector");

+ 2 - 2
src/data-projector.d.ts

@@ -15,13 +15,13 @@ export class DataProjector extends Service {
   defineSchema(schemaDef: ProjectionSchemaDefinition): this;
   defineSchema(schemaDef: ProjectionSchemaDefinition): this;
 
 
   /**
   /**
-   * Project data.
+   * Project.
    *
    *
    * @param data
    * @param data
    * @param schema
    * @param schema
    * @param options
    * @param options
    */
    */
-  projectData<T>(
+  project<T>(
     data: T,
     data: T,
     schema: ProjectionSchema,
     schema: ProjectionSchema,
     options?: ProjectDataOptions,
     options?: ProjectDataOptions,

+ 4 - 4
src/data-projector.js

@@ -18,14 +18,14 @@ export class DataProjector extends Service {
   }
   }
 
 
   /**
   /**
-   * Project data.
+   * Project.
    *
    *
    * @param {object|object[]|*} data
    * @param {object|object[]|*} data
    * @param {object|Function|string} schema
    * @param {object|Function|string} schema
    * @param {object} [options]
    * @param {object} [options]
    * @returns {*}
    * @returns {*}
    */
    */
-  projectData(data, schema, options) {
+  project(data, schema, options) {
     const registry = this.getService(ProjectionSchemaRegistry);
     const registry = this.getService(ProjectionSchemaRegistry);
     const defaultNameResolver = name => registry.getSchema(name);
     const defaultNameResolver = name => registry.getSchema(name);
     const nameResolver =
     const nameResolver =
@@ -43,7 +43,7 @@ export class DataProjector extends Service {
    * @returns {*}
    * @returns {*}
    */
    */
   projectInput(data, schema, options) {
   projectInput(data, schema, options) {
-    return this.projectData(data, schema, {...options, scope: 'input'});
+    return this.project(data, schema, {...options, scope: 'input'});
   }
   }
 
 
   /**
   /**
@@ -55,6 +55,6 @@ export class DataProjector extends Service {
    * @returns {*}
    * @returns {*}
    */
    */
   projectOutput(data, schema, options) {
   projectOutput(data, schema, options) {
-    return this.projectData(data, schema, {...options, scope: 'output'});
+    return this.project(data, schema, {...options, scope: 'output'});
   }
   }
 }
 }

+ 11 - 11
src/data-projector.spec.js

@@ -39,24 +39,24 @@ describe('DataProjector', function () {
     });
     });
   });
   });
 
 
-  describe('projectData', function () {
+  describe('project', function () {
     it('should project the data object by the given schema', function () {
     it('should project the data object by the given schema', function () {
       const S = new DataProjector();
       const S = new DataProjector();
-      const res = S.projectData({foo: 10, bar: 20}, {foo: true, bar: false});
+      const res = S.project({foo: 10, bar: 20}, {foo: true, bar: false});
       expect(res).to.be.eql({foo: 10});
       expect(res).to.be.eql({foo: 10});
     });
     });
 
 
     it('should project the data object by the schema name', function () {
     it('should project the data object by the schema name', function () {
       const S = new DataProjector();
       const S = new DataProjector();
       S.defineSchema({name: 'mySchema', schema: {foo: true, bar: false}});
       S.defineSchema({name: 'mySchema', schema: {foo: true, bar: false}});
-      const res = S.projectData({foo: 10, bar: 20, baz: 30}, 'mySchema');
+      const res = S.project({foo: 10, bar: 20, baz: 30}, 'mySchema');
       expect(res).to.be.eql({foo: 10, baz: 30});
       expect(res).to.be.eql({foo: 10, baz: 30});
     });
     });
 
 
     it('should exclude properties without rules in the strict mode', function () {
     it('should exclude properties without rules in the strict mode', function () {
       const S = new DataProjector();
       const S = new DataProjector();
       S.defineSchema({name: 'mySchema', schema: {foo: true, bar: false}});
       S.defineSchema({name: 'mySchema', schema: {foo: true, bar: false}});
-      const res = S.projectData({foo: 10, bar: 20, baz: 30}, 'mySchema', {
+      const res = S.project({foo: 10, bar: 20, baz: 30}, 'mySchema', {
         strict: true,
         strict: true,
       });
       });
       expect(res).to.be.eql({foo: 10});
       expect(res).to.be.eql({foo: 10});
@@ -71,7 +71,7 @@ describe('DataProjector', function () {
         expect(name).to.be.eq(schemaName);
         expect(name).to.be.eq(schemaName);
         return {foo: true, bar: false};
         return {foo: true, bar: false};
       };
       };
-      const res = S.projectData({foo: 10, bar: 20}, 'mySchema', {nameResolver});
+      const res = S.project({foo: 10, bar: 20}, 'mySchema', {nameResolver});
       expect(res).to.be.eql({foo: 10});
       expect(res).to.be.eql({foo: 10});
       expect(invoked).to.be.eq(1);
       expect(invoked).to.be.eq(1);
     });
     });
@@ -84,7 +84,7 @@ describe('DataProjector', function () {
         expect(container).to.be.eq(S.container);
         expect(container).to.be.eq(S.container);
         return {foo: true, bar: false};
         return {foo: true, bar: false};
       };
       };
-      const res = S.projectData({foo: 10, bar: 20}, factory);
+      const res = S.project({foo: 10, bar: 20}, factory);
       expect(res).to.be.eql({foo: 10});
       expect(res).to.be.eql({foo: 10});
       expect(invoked).to.be.eq(1);
       expect(invoked).to.be.eq(1);
     });
     });
@@ -98,21 +98,21 @@ describe('DataProjector', function () {
         expect(args).to.be.eql(factoryArgs);
         expect(args).to.be.eql(factoryArgs);
         return {foo: true, bar: false};
         return {foo: true, bar: false};
       };
       };
-      const res = S.projectData({foo: 10, bar: 20}, factory, {factoryArgs});
+      const res = S.project({foo: 10, bar: 20}, factory, {factoryArgs});
       expect(res).to.be.eql({foo: 10});
       expect(res).to.be.eql({foo: 10});
       expect(invoked).to.be.eq(1);
       expect(invoked).to.be.eq(1);
     });
     });
   });
   });
 
 
   describe('projectInput', function () {
   describe('projectInput', function () {
-    it('should invoke the "projectData" method with the "input" scope and return its result', function () {
+    it('should invoke the "project" method with the "input" scope and return its result', function () {
       const S = new DataProjector();
       const S = new DataProjector();
       const schema = {foo: true, bar: false};
       const schema = {foo: true, bar: false};
       const data = {foo: 10, bar: 20};
       const data = {foo: 10, bar: 20};
       const options = {extra: true};
       const options = {extra: true};
       const result = {foo: 10};
       const result = {foo: 10};
       let invoked = 0;
       let invoked = 0;
-      S.projectData = (...args) => {
+      S.project = (...args) => {
         expect(args).to.be.eql([data, schema, {extra: true, scope: 'input'}]);
         expect(args).to.be.eql([data, schema, {extra: true, scope: 'input'}]);
         invoked++;
         invoked++;
         return result;
         return result;
@@ -135,14 +135,14 @@ describe('DataProjector', function () {
   });
   });
 
 
   describe('projectOutput', function () {
   describe('projectOutput', function () {
-    it('should invoke the "projectData" method with the "output" scope and return its result', function () {
+    it('should invoke the "project" method with the "output" scope and return its result', function () {
       const S = new DataProjector();
       const S = new DataProjector();
       const schema = {foo: true, bar: false};
       const schema = {foo: true, bar: false};
       const data = {foo: 10, bar: 20};
       const data = {foo: 10, bar: 20};
       const options = {extra: true};
       const options = {extra: true};
       const result = {foo: 10};
       const result = {foo: 10};
       let invoked = 0;
       let invoked = 0;
-      S.projectData = (...args) => {
+      S.project = (...args) => {
         expect(args).to.be.eql([data, schema, {extra: true, scope: 'output'}]);
         expect(args).to.be.eql([data, schema, {extra: true, scope: 'output'}]);
         invoked++;
         invoked++;
         return result;
         return result;