Skip to content

Commit

Permalink
refactor(all): used deep-extend replace deep copy, close #412
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed May 6, 2018
1 parent a729418 commit 44ba251
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 130 deletions.
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
/src
/site
18 changes: 9 additions & 9 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ set -u -e -o pipefail
readonly currentDir=$(cd $(dirname $0); pwd)
cd ${currentDir}

PACKAGES=(cli)
# PACKAGES=(acl
# theme
# abc
# auth
# cache
# mock
# form
# cli)
# PACKAGES=(form)
PACKAGES=(acl
theme
abc
auth
cache
mock
form
cli)
NODE_PACKAGES=(cli)

#######################################
Expand Down
2 changes: 2 additions & 0 deletions docs/style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ ng-alain 使用 [Prettier](https://prettier.io/) 来优化代码风格,并且
`lint` 可以非常有效的帮助我们更早发现bug、更高的可读性;如果我们能够保证团队开发过程中每一次 commit 前都自动做一次 staged 中文件的 lint 的话,那不是非常酷吗?

ng-alain 配置了每次对 staged 进行 commit 时会预先做 lint,若发现错误则无法提交。

默认开启了 `*.ts``*.less` 的提交过程中强制对代码进行格式化,你可以通过修改 `package.json``lint-staged` 节点来改变些规则(例如:`prettier --write`)。
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"precommit": "npm run lint-staged",
"analyze": "ng build --prod --build-optimizer --stats-json",
"prepush": "npm run lint",
"lint": "run-s lint:ts lint:style",
"lint:ts": "tslint -p tsconfig.json -c tslint.json 'packages/**/*.ts' -e 'packages/**/*.spec.ts' -e 'packages/cli/**/*'",
"lint": "npm run lint:ts && npm run lint:style",
"lint:ts": "tslint -p tsconfig.json -c tslint.json 'packages/**/*.ts'",
"lint:style": "stylelint \"{packages}/**/*.less\" --syntax less",
"tslint-check": "tslint-config-prettier-check ./tslint.json",
"lint-staged": "lint-staged",
Expand Down Expand Up @@ -57,6 +57,7 @@
"@antv/g2-plugin-slider": "^2.0.0",
"@ngx-translate/core": "^9.0.1",
"@stackblitz/sdk": "^1.1.1",
"deep-extend": "^0.5.1",
"file-saver": "^1.3.3",
"marked": "^0.3.9",
"ng-zorro-antd": "^0.7.0-beta.5",
Expand All @@ -73,6 +74,7 @@
"@angular/compiler-cli": "^5.0.0",
"@angular/language-service": "^5.0.0",
"@schematics/schematics": "^0.4.6",
"@types/deep-extend": "^0.4.31",
"@types/fs-extra": "^5.0.1",
"@types/gulp": "^4.0.4",
"@types/htmlparser2": "^3.7.31",
Expand All @@ -85,15 +87,15 @@
"chalk": "^2.3.0",
"codecov": "^3.0.0",
"codelyzer": "~4.2.1",
"editorconfig-tools": "^0.1.1",
"husky": "^0.14.3",
"del": "^3.0.0",
"docsearch.js": "^2.5.2",
"domhandler": "^2.4.1",
"editorconfig-tools": "^0.1.1",
"enquire.js": "^2.1.6",
"fs-extra": "^5.0.0",
"gh-pages": "^1.1.0",
"htmlparser2": "^3.9.2",
"husky": "^0.14.3",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"jsonml.js": "^0.1.0",
Expand All @@ -103,30 +105,30 @@
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"klaw-sync": "^3.0.2",
"lint-staged": "^7.0.5",
"mark-twain": "^2.0.2",
"mkdirp": "^0.5.1",
"lint-staged": "^7.0.5",
"mockjs": "^1.0.1-beta3",
"mustache": "^2.3.0",
"ng-github-button": "^0.0.1",
"ng2-ace-editor": "^0.3.7",
"ngx-tinymce": "^1.0.1",
"ngx-ueditor": "^1.1.0",
"prettier": "^1.12.1",
"prettier-stylelint": "^0.4.2",
"protractor": "~5.2.2",
"rollup": "^0.49.2",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-uglify": "^2.0.1",
"prettier": "^1.12.1",
"prettier-stylelint": "^0.4.2",
"stylelint": "^8.2.0",
"stylelint-config-standard": "^18.0.0",
"through2": "^2.0.3",
"ts-node": "~4.1.0",
"tslint": "~5.8.0",
"tslint-language-service": "^0.9.6",
"tslint-config-prettier": "^1.12.0",
"tslint-language-service": "^0.9.6",
"typescript": "~2.6.0",
"webpack-bundle-analyzer": "^2.9.2",
"xlsx": "^0.11.16"
Expand All @@ -138,12 +140,10 @@
],
"packages/**/*.ts": [
"npm run lint:ts",
"prettier --write",
"git add"
],
"packages/**/*.less": [
"npm run lint:style",
"prettier --write",
"git add"
]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/abc/image/image.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ImageDirective implements OnChanges, OnInit {
private render: Renderer2,
DEF: AdImageConfig,
) {
Object.assign(this, deepCopy(DEF));
Object.assign(this, deepCopy({}, DEF));
}

ngOnInit(): void {
Expand Down
3 changes: 2 additions & 1 deletion packages/abc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@delon/theme": "PEER-0.0.0-PLACEHOLDER",
"@delon/acl": "PEER-0.0.0-PLACEHOLDER",
"file-saver": "^1.3.3",
"ngx-countdown": "^2.1.0"
"ngx-countdown": "^2.1.0",
"deep-extend": "^0.5.1"
}
}
3 changes: 3 additions & 0 deletions packages/abc/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const globals = {
'date-fns/sub_years': 'date-fns/sub_years/index',
'date-fns/add_days': 'date-fns/add_days/index',

'deep-extend': 'deep-extend',

'@delon/theme': 'alain.theme',
'@delon/acl': 'alain.acl',
};
Expand All @@ -70,6 +72,7 @@ const listOfDateFns = [
'endOfYear',
'subYears',
'addDays',
'deepExtend',
];

const listOfReplace = listOfDateFns.map(name => {
Expand Down
8 changes: 4 additions & 4 deletions packages/abc/sidebar-nav/sidebar-nav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('abc: sidebar-nav', () => {
router = injector.get(Router);
setSrv = injector.get(SettingsService);
menuSrv = injector.get(MenuService);
menuSrv.add(deepCopy(MOCKMENUS));
menuSrv.add(deepCopy([], MOCKMENUS));
page = new PageObject();
if (needMockNavigateByUrl) spyOn(router, 'navigateByUrl');
if (callback) callback();
Expand All @@ -96,7 +96,7 @@ describe('abc: sidebar-nav', () => {
it('should be navigate url', () => {
createComp();
spyOn(context, 'select');
const data = deepCopy(MOCKMENUS);
const data = deepCopy([], MOCKMENUS);
menuSrv.add(data);
expect(context.select).not.toHaveBeenCalled();
expect(router.navigateByUrl).not.toHaveBeenCalled();
Expand All @@ -109,7 +109,7 @@ describe('abc: sidebar-nav', () => {

it('should be toggle open', () => {
createComp();
const data = deepCopy(MOCKMENUS);
const data = deepCopy([], MOCKMENUS);
menuSrv.add(data);
expect(data[0].children[0]._open).toBeUndefined();
const subTitleEl = getEl<HTMLElement>('.nav-sub-title');
Expand All @@ -121,7 +121,7 @@ describe('abc: sidebar-nav', () => {
it('should be reset menu when service is changed', () => {
createComp();
isText('.nav-group-title', MOCKMENUS[0].text);
const newMenu = deepCopy(MOCKMENUS);
const newMenu = deepCopy([], MOCKMENUS);
newMenu[0].text = 'new主导航';
menuSrv.add(newMenu);
fixture.detectChanges();
Expand Down
22 changes: 13 additions & 9 deletions packages/abc/simple-table/simple-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
SimpleChange,
QueryList,
ViewChildren,
AfterViewInit,
ContentChildren,
ContentChild,
Optional,
Expand Down Expand Up @@ -65,8 +64,9 @@ import { SimpleTableExport } from './simple-table-export';
preserveWhitespaces: false,
})
export class SimpleTableComponent
implements OnInit, OnChanges, AfterViewInit, OnDestroy {
implements OnInit, OnChanges, OnDestroy {
private data$: Subscription;
private _inited = false;
_data: SimpleTableData[] = [];
_url: string;
_isAjax = false;
Expand Down Expand Up @@ -333,7 +333,7 @@ export class SimpleTableComponent
private number: DecimalPipe,
@Inject(DOCUMENT) private doc: any,
) {
Object.assign(this, deepCopy(defConfig));
Object.assign(this, deepCopy({}, defConfig));
}

// region: data
Expand Down Expand Up @@ -399,6 +399,7 @@ export class SimpleTableComponent
}

_change(type: 'pi' | 'ps') {
if (!this._inited) return ;
this._genAjax();
this._genData();
this._toTop();
Expand Down Expand Up @@ -842,9 +843,11 @@ export class SimpleTableComponent

// endregion

ngOnInit(): void {}

ngAfterViewInit(): void {}
ngOnInit(): void {
this._inited = true;
this.updateColumns();
this.processData();
}

private setClass() {
this._classMap.forEach(cls =>
Expand All @@ -869,7 +872,8 @@ export class SimpleTableComponent
const sortMap: Object = {};
let idx = 0;
const newColumns: SimpleTableColumn[] = [];
for (const item of this.columns) {
const copyColumens = deepCopy([], this.columns);
for (const item of copyColumens) {
if (this.acl && item.acl && !this.acl.can(item.acl)) continue;
if (item.index) {
if (!Array.isArray(item.index)) item.index = item.index.split('.');
Expand Down Expand Up @@ -977,8 +981,8 @@ export class SimpleTableComponent
ngOnChanges(
changes: { [P in keyof this]?: SimpleChange } & SimpleChanges,
): void {
if (changes.columns) this.updateColumns();
if (changes.data) this.processData();
if (changes.columns && this._inited) this.updateColumns();
if (changes.data && this._inited) this.processData();

this.setClass();
}
Expand Down
7 changes: 4 additions & 3 deletions packages/abc/simple-table/simple-table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from './interface';
import { AdSimpleTableModule } from './simple-table.module';
import { SimpleTableComponent } from './simple-table.component';
import { deepCopy } from '..';

const USERS: any[] = Array(100)
.fill({})
Expand Down Expand Up @@ -256,7 +257,7 @@ describe('abc: simple-table', () => {
describe('static data', () => {
it('should be not pagination when data length less than ps value', () => {
genModule({
template: `<simple-table #st [data]="data" [ps]="ps"></simple-table>`,
template: `<simple-table #st [data]="data" [columns]="columns" [ps]="ps"></simple-table>`,
});
context.data = USERS.slice(0, 3);
context.ps = 4;
Expand Down Expand Up @@ -379,7 +380,7 @@ describe('abc: simple-table', () => {
})
class TestComponent {
@ViewChild('st') comp: SimpleTableComponent;
data: string | any[] = JSON.parse(JSON.stringify(USERS));
data: string | any[] = deepCopy([], USERS);
extraParams: any;
reqMehtod: string;
reqBody: any;
Expand All @@ -388,7 +389,7 @@ class TestComponent {
reqError() {}
resReName: ResReNameType;
ps = PS;
columns: SimpleTableColumn[] = JSON.parse(JSON.stringify(COLUMNS));
columns: SimpleTableColumn[] = deepCopy([], COLUMNS);
pi: number;
total: number;
showPagination = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/abc/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ describe('abc: utils', () => {
});

it('#deepCopy', () => {
const a = 1;
expect(deepCopy(a)).toBe(a);
const a = { number: 1 };
expect(deepCopy({}, a).number).toBe(a.number);
});

describe('#copy', () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/abc/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as startOfYear from 'date-fns/start_of_year';
import * as endOfYear from 'date-fns/end_of_year';
import * as subYears from 'date-fns/sub_years';
import * as addDays from 'date-fns/add_days';
import * as deepExtend from 'deep-extend';

/**
* 转化成RMB元字符串
Expand Down Expand Up @@ -87,9 +88,9 @@ export function deepGet(obj: any, path: string[], defaultValue?: any) {
return path.reduce((o, k) => o[k], obj) || defaultValue;
}

export function deepCopy(obj: any) {
// BAD: a temporary solution
return JSON.parse(JSON.stringify(obj));
export function deepCopy(target: any, obj: any) {
const result = deepExtend({ }, { __source: obj });
return result.__source;
}

/** 复制内容至剪贴板 */
Expand Down
3 changes: 2 additions & 1 deletion packages/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"ng-zorro-antd"
],
"peerDependencies": {
"ng-zorro-antd": "ZORRO-0.0.0-PLACEHOLDER"
"ng-zorro-antd": "ZORRO-0.0.0-PLACEHOLDER",
"deep-extend": "^0.5.1"
}
}
6 changes: 5 additions & 1 deletion packages/form/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ const globals = {
'rxjs/observable/combineLatest': 'Rx.Observable',

'date-fns/format': 'date-fns/format/index',
'deep-extend': 'deep-extend',
};

const listOfDateFns = ['format'];
const listOfDateFns = [
'format',
'deepExtend'
];

const listOfReplace = listOfDateFns.map(name => {
const map = {};
Expand Down
4 changes: 2 additions & 2 deletions packages/form/spec/form.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebugElement } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';
import { deepCopy } from '@delon/abc';
import * as deepExtend from 'deep-extend';
import { builder, TestFormComponent, SFPage, SCHEMA } from './base.spec';
import { SFSchema } from '../src/schema/index';
import { SFUISchemaItem, SFUISchema } from '../src/schema/ui';
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('form: component', () => {

describe('#reset', () => {
it('should be set default value', () => {
const schema = deepCopy(SCHEMA.user) as SFSchema;
const schema = deepExtend({}, SCHEMA.user) as SFSchema;
schema.properties.name.default = 'cipchk';
page
.newSchema(schema)
Expand Down
Loading

0 comments on commit 44ba251

Please sign in to comment.