From f1047f2d07ca5d84f5fd60ca8ae963061315b527 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Mon, 31 Jul 2023 12:48:18 +0300 Subject: [PATCH] add ability to extend multiple parents --- CHANGELOG.md | 10 ++- packages/fetcher/package.json | 2 +- packages/jac/package.json | 2 +- packages/reactivity/package.json | 2 +- packages/reactivity/src/hooks/extend.test.ts | 72 ++++++++++++++++---- packages/reactivity/src/hooks/extend.ts | 15 ++-- packages/reactivity/src/types/extend.ts | 16 +++-- packages/tools/package.json | 2 +- packages/w3p/package.json | 2 +- 9 files changed, 93 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19930127..dec7cb77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## [1.0.0-rc.3] - 2023-07-31 +### Added +- `@distributedlab/reactity` - `extend` ability to extend multiple parents + +### Changed +- `@distributedlab/reactity` - `extend` arguments order + ## [1.0.0-rc.2] - 2023-07-17 ### Fixed - `@distributedlab/tools` - `Time` `TimeDate` data when the value is in timestamp format @@ -304,7 +311,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [old repo]: https://github.com/distributed-lab/web-kit-old -[Unreleased]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.2...HEAD +[Unreleased]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.3...HEAD +[1.0.0-rc.3]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.2...1.0.0-rc.3 [1.0.0-rc.2]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.1...1.0.0-rc.2 [1.0.0-rc.1]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.0...1.0.0-rc.1 [1.0.0-rc.0]: https://github.com/distributed-lab/web-kit/compare/0.2.0...1.0.0-rc.0 diff --git a/packages/fetcher/package.json b/packages/fetcher/package.json index 4589e46c..ba5a3288 100644 --- a/packages/fetcher/package.json +++ b/packages/fetcher/package.json @@ -1,6 +1,6 @@ { "name": "@distributedlab/fetcher", - "version": "1.0.0-rc.2", + "version": "1.0.0-rc.3", "description": "Fetch API wrapper with the extended functionality and simple interface", "repository": { "type": "git", diff --git a/packages/jac/package.json b/packages/jac/package.json index 7245b2c3..a226ce07 100644 --- a/packages/jac/package.json +++ b/packages/jac/package.json @@ -1,6 +1,6 @@ { "name": "@distributedlab/jac", - "version": "1.0.0-rc.2", + "version": "1.0.0-rc.3", "description": "A library for constructing JSON-API compliant requests and responses", "repository": { "type": "git", diff --git a/packages/reactivity/package.json b/packages/reactivity/package.json index a0c95d76..61ad50f5 100644 --- a/packages/reactivity/package.json +++ b/packages/reactivity/package.json @@ -1,6 +1,6 @@ { "name": "@distributedlab/reactivity", - "version": "1.0.0-rc.2", + "version": "1.0.0-rc.3", "description": "Implementation of the reactivity connections to propagate changes between objects", "repository": { "type": "git", diff --git a/packages/reactivity/src/hooks/extend.test.ts b/packages/reactivity/src/hooks/extend.test.ts index 56ce160d..3c4f0613 100644 --- a/packages/reactivity/src/hooks/extend.test.ts +++ b/packages/reactivity/src/hooks/extend.test.ts @@ -6,6 +6,7 @@ import { ref } from './ref' const mockedAge = 10 const changedAge = 20 const mockedName = 'John' +const mockedSex = 'male' describe('performs extend hook unit test', () => { test('should basically extend child with parent', () => { @@ -20,9 +21,12 @@ describe('performs extend hook unit test', () => { const name = computed(() => mockedName) return toRaw( - extend(parent, { - name, - }), + extend( + { + name, + }, + parent, + ), ) } @@ -53,9 +57,12 @@ describe('performs extend hook unit test', () => { const name = computed(() => mockedName) return toRaw( - extend(parent, { - name, - }), + extend( + { + name, + }, + parent, + ), ) } @@ -84,9 +91,12 @@ describe('performs extend hook unit test', () => { const name = computed(() => mockedName) return toRaw( - extend(parent, { - name, - }), + extend( + { + name, + }, + parent, + ), ) } @@ -111,9 +121,12 @@ describe('performs extend hook unit test', () => { const age = ref(changedAge) return toRaw( - extend(parent, { - age, - }), + extend( + { + age, + }, + parent, + ), ) } @@ -121,4 +134,39 @@ describe('performs extend hook unit test', () => { expect(child.age).toBe(changedAge) }) + + test('should extend multiple parents', () => { + const createParent1 = () => { + const age = ref(mockedAge) + + return toRaw({ + age, + }) + } + + const createParent2 = () => { + const name = ref(mockedName) + + return toRaw({ + name, + }) + } + + const createChild = () => { + const parent1 = createParent1() + const parent2 = createParent2() + const sex = ref(mockedSex) + + return toRaw(extend({ sex }, parent1, parent2)) + } + + const child = createChild() + + expect(child).toHaveProperty('age') + expect(child).toHaveProperty('name') + expect(child).toHaveProperty('sex') + expect(child.age).toBe(mockedAge) + expect(child.name).toBe(mockedName) + expect(child.sex).toBe(mockedSex) + }) }) diff --git a/packages/reactivity/src/hooks/extend.ts b/packages/reactivity/src/hooks/extend.ts index 6ef7e1b0..9e41f25a 100644 --- a/packages/reactivity/src/hooks/extend.ts +++ b/packages/reactivity/src/hooks/extend.ts @@ -1,15 +1,18 @@ -import type { Extended, Raw } from '@/types' +import type { Extended, Parent } from '@/types' import { computed } from './computed' import { unref } from './ref' -export const extend =

( - parent: Raw

, +export const extend = ( child: C, -): Extended => { - const obj = {} as Extended + ...parents: [...P] +): Extended => { + const obj = {} as Extended + + parents.forEach(p => { + Object.defineProperties(obj, createDescriptors(p)) + }) - Object.defineProperties(obj, createDescriptors(parent)) Object.defineProperties(obj, createDescriptors(child)) return obj diff --git a/packages/reactivity/src/types/extend.ts b/packages/reactivity/src/types/extend.ts index 420b7d84..b29e7367 100644 --- a/packages/reactivity/src/types/extend.ts +++ b/packages/reactivity/src/types/extend.ts @@ -1,7 +1,11 @@ -import type { Raw } from './raw' +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type Parent = T -export type Extended

= Omit< - Raw

, - keyof C -> & - C +export type UnionToIntersection = ( + U extends object ? (k: U) => void : never +) extends (k: infer I) => void + ? I + : never + +export type Extended = C & + Omit, keyof C> diff --git a/packages/tools/package.json b/packages/tools/package.json index 045e9e47..d5b880fa 100644 --- a/packages/tools/package.json +++ b/packages/tools/package.json @@ -1,6 +1,6 @@ { "name": "@distributedlab/tools", - "version": "1.0.0-rc.2", + "version": "1.0.0-rc.3", "description": "Collection of common utility functions and classes", "repository": { "type": "git", diff --git a/packages/w3p/package.json b/packages/w3p/package.json index 60a26215..bd0bb034 100644 --- a/packages/w3p/package.json +++ b/packages/w3p/package.json @@ -1,6 +1,6 @@ { "name": "@distributedlab/w3p", - "version": "1.0.0-rc.2", + "version": "1.0.0-rc.3", "description": "Wrapper for Web3 Providers", "repository": { "type": "git",