diff --git a/README.md b/README.md index eea99e8..fe88155 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Angular extensions powered by community. Package Name | Features ------------------------- | ------------------------------------------- +`@angular-contrib/core` | [`IterableDiffers.extend`][IterableDiffersExtensibility], [`KeyValueDiffers.extend`][KeyValueDiffersExtensibility] `@angular-contrib/common` | [``][NgHost], [`ngForIn`][NgForIn], [`ngSwitchCaseContinue`][NgSwitchContinue] ## Usage @@ -54,6 +55,8 @@ class AppComponent { } + Any feature once implemented by Angular will be deprecated here; +[IterableDiffersExtensibility]: https://github.com/trotyl/angular-contrib/tree/master/packages/core/iterable-differs +[KeyValueDiffersExtensibility]: https://github.com/trotyl/angular-contrib/tree/master/packages/core/key-value-differs [NgForIn]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/for-in [NgHost]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/host [NgSwitchContinue]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/switch-continue diff --git a/package.json b/package.json index b6d9493..9b88613 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", + "build:core": "ng-packagr -p packages/core/package.json", "build:common": "ng-packagr -p packages/common/package.json" }, "private": true, diff --git a/packages/core/index.ts b/packages/core/index.ts new file mode 100644 index 0000000..7bbfb9c --- /dev/null +++ b/packages/core/index.ts @@ -0,0 +1,2 @@ +export * from './iterable-differs/index'; +export * from './key-value-differs/index'; diff --git a/packages/core/iterable-differs/README.md b/packages/core/iterable-differs/README.md new file mode 100644 index 0000000..d78c8d6 --- /dev/null +++ b/packages/core/iterable-differs/README.md @@ -0,0 +1,34 @@ +# IterableDiffers Extensibility + +Supports extending `IterableDiffers` with custom differ implementations. + +## Type + +**InjectionToken** + +## Provenance + ++ https://github.com/angular/angular/issues/11309 + +## NgModule + +`@angular-contrib/core#IterableDiffersModule` + +## Usage + +Providing custom `IterableDifferFactory`: + +```typescript +import { IterableDiffersModule } from '@angular-contrib/core'; + +const customIterableDifferFactories = [ FastDifferFactory ]; + +@NgModule({ + imports: [ IterableDiffersModule.extend(customIterableDifferFactories) ], +}) +class MyModule { } +``` + +## Note + ++ Can only be provided in root scope; diff --git a/packages/core/iterable-differs/index.ts b/packages/core/iterable-differs/index.ts new file mode 100644 index 0000000..2f11cd4 --- /dev/null +++ b/packages/core/iterable-differs/index.ts @@ -0,0 +1,2 @@ +export * from './iterable-differs'; +export * from './iterable-differs.module'; diff --git a/packages/core/iterable-differs/iterable-differs.module.ts b/packages/core/iterable-differs/iterable-differs.module.ts new file mode 100644 index 0000000..b81fe54 --- /dev/null +++ b/packages/core/iterable-differs/iterable-differs.module.ts @@ -0,0 +1,21 @@ +import { Inject, IterableDiffers, IterableDifferFactory, ModuleWithProviders, NgModule } from '@angular/core'; +import { ITERABLE_DIFFER_FACTORIES } from './iterable-differs'; + +@NgModule() +export class IterableDiffersModule { + static extend(factories: IterableDifferFactory[]): ModuleWithProviders { + return { + ngModule: IterableDiffersModule, + providers: [ + { provide: ITERABLE_DIFFER_FACTORIES, useValue: factories }, + ], + }; + } + + constructor( + iterableDiffers: IterableDiffers, + @Inject(ITERABLE_DIFFER_FACTORIES) extraIterableDifferFactories: IterableDifferFactory[], + ) { + iterableDiffers.factories.splice(0, 0, ...extraIterableDifferFactories); + } +} diff --git a/packages/core/iterable-differs/iterable-differs.spec.ts b/packages/core/iterable-differs/iterable-differs.spec.ts new file mode 100644 index 0000000..175c699 --- /dev/null +++ b/packages/core/iterable-differs/iterable-differs.spec.ts @@ -0,0 +1,27 @@ +import { Component, IterableDiffer, IterableDiffers, IterableDifferFactory } from '@angular/core'; +import { async, inject, TestBed } from '@angular/core/testing'; +import { IterableDiffersModule } from './iterable-differs.module'; + +describe('IterableDiffers Extensibility', () => { + let spyIterableDifferFactory: IterableDifferFactory; + + beforeEach(() => { + spyIterableDifferFactory = { + supports(value: any): boolean { return true; }, + create(): IterableDiffer { return { value: 42 } as any; }, + }; + }); + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [IterableDiffersModule.extend([spyIterableDifferFactory])], + }).compileComponents(); + })); + + it('should use custom IterableDifferFactory', inject([IterableDiffers], (differs: IterableDiffers) => { + const differ = differs.find([1, 2]).create(); + + expect(differ).toEqual({ value: 42 } as any); + })); + +}); diff --git a/packages/core/iterable-differs/iterable-differs.ts b/packages/core/iterable-differs/iterable-differs.ts new file mode 100644 index 0000000..f377e0e --- /dev/null +++ b/packages/core/iterable-differs/iterable-differs.ts @@ -0,0 +1,3 @@ +import { InjectionToken, IterableDifferFactory } from '@angular/core'; + +export const ITERABLE_DIFFER_FACTORIES = new InjectionToken('ITERABLE_DIFFER_FACTORIES'); diff --git a/packages/core/key-value-differs/README.md b/packages/core/key-value-differs/README.md new file mode 100644 index 0000000..6958a44 --- /dev/null +++ b/packages/core/key-value-differs/README.md @@ -0,0 +1,34 @@ +# KeyValueDiffers Extensibility + +Supports extending `KeyValueDiffers` with custom differ implementations. + +## Type + +**InjectionToken** + +## Provenance + ++ https://github.com/angular/angular/issues/11309 + +## NgModule + +`@angular-contrib/core#KeyValueDiffersModule` + +## Usage + +Providing custom `KeyValueDifferFactory`: + +```typescript +import { KeyValueDiffersModule } from '@angular-contrib/core'; + +const customKeyValueDifferFactories = [ FastDifferFactory ]; + +@NgModule({ + imports: [ KeyValueDiffersModule.extend(customKeyValueDifferFactories) ], +}) +class MyModule { } +``` + +## Note + ++ Can only be provided in root scope; diff --git a/packages/core/key-value-differs/index.ts b/packages/core/key-value-differs/index.ts new file mode 100644 index 0000000..c224fca --- /dev/null +++ b/packages/core/key-value-differs/index.ts @@ -0,0 +1,2 @@ +export * from './key-value-differs'; +export * from './key-value-differs.module'; diff --git a/packages/core/key-value-differs/key-value-differs.module.ts b/packages/core/key-value-differs/key-value-differs.module.ts new file mode 100644 index 0000000..d654e74 --- /dev/null +++ b/packages/core/key-value-differs/key-value-differs.module.ts @@ -0,0 +1,21 @@ +import { Inject, KeyValueDiffers, KeyValueDifferFactory, ModuleWithProviders, NgModule } from '@angular/core'; +import { KEY_VALUE_DIFFER_FACTORIES } from './key-value-differs'; + +@NgModule() +export class KeyValueDiffersModule { + static extend(factories: KeyValueDifferFactory[]): ModuleWithProviders { + return { + ngModule: KeyValueDiffersModule, + providers: [ + { provide: KEY_VALUE_DIFFER_FACTORIES, useValue: factories }, + ], + }; + } + + constructor( + keyValueDiffers: KeyValueDiffers, + @Inject(KEY_VALUE_DIFFER_FACTORIES) extraKeyValueDifferFactories: KeyValueDifferFactory[], + ) { + keyValueDiffers.factories.splice(0, 0, ...extraKeyValueDifferFactories); + } +} diff --git a/packages/core/key-value-differs/key-value-differs.spec.ts b/packages/core/key-value-differs/key-value-differs.spec.ts new file mode 100644 index 0000000..aa1d653 --- /dev/null +++ b/packages/core/key-value-differs/key-value-differs.spec.ts @@ -0,0 +1,27 @@ +import { Component, KeyValueDiffer, KeyValueDiffers, KeyValueDifferFactory } from '@angular/core'; +import { async, inject, TestBed } from '@angular/core/testing'; +import { KeyValueDiffersModule } from './key-value-differs.module'; + +describe('KeyValueDiffers Extensibility', () => { + let spyKeyValueDifferFactory: KeyValueDifferFactory; + + beforeEach(() => { + spyKeyValueDifferFactory = { + supports(value: any): boolean { return true; }, + create(): KeyValueDiffer { return { value: 42 } as any; }, + }; + }); + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [KeyValueDiffersModule.extend([spyKeyValueDifferFactory])], + }).compileComponents(); + })); + + it('should use custom KeyValueDifferFactory', inject([KeyValueDiffers], (differs: KeyValueDiffers) => { + const differ = differs.find([1, 2]).create(); + + expect(differ).toEqual({ value: 42 } as any); + })); + +}); diff --git a/packages/core/key-value-differs/key-value-differs.ts b/packages/core/key-value-differs/key-value-differs.ts new file mode 100644 index 0000000..76f065a --- /dev/null +++ b/packages/core/key-value-differs/key-value-differs.ts @@ -0,0 +1,3 @@ +import { InjectionToken, KeyValueDifferFactory } from '@angular/core'; + +export const KEY_VALUE_DIFFER_FACTORIES = new InjectionToken('KEY_VALUE_DIFFER_FACTORIES'); diff --git a/packages/core/package.json b/packages/core/package.json new file mode 100644 index 0000000..2e2999b --- /dev/null +++ b/packages/core/package.json @@ -0,0 +1,23 @@ +{ + "name": "@angular-contrib/core", + "version": "0.0.1-alpha.2", + "description": "", + "repository": { + "type": "git", + "url": "https://github.com/trotyl/angular-contrib" + }, + "keywords": [ + "angular" + ], + "author": "Trotyl Yu ", + "license": "MIT", + "ngPackage": { + "lib": { + "entryFile": "index.ts" + }, + "dest": "../../publish/core" + }, + "peerDependencies": { + "@angular/core": "^5.0.0" + } +}