From e1343c6fb175caf44fa842ac98929d828abecd26 Mon Sep 17 00:00:00 2001 From: Krzysztof Miemiec Date: Sat, 7 Jan 2017 15:45:05 +0100 Subject: [PATCH] =?UTF-8?q?Support=20for=20=E2=80=9EstrictNullChecks?= =?UTF-8?q?=E2=80=9D=20in=20tsconfig=20=20(#170)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Support for „strictNullChecks” in tsconfig * Added flag in `tsconfig` * Modified `ng-uploader-options` to use null-strict values: variables that have defaults must always be defined, variables without defaults are allowed to be undefined (but not null) * Modified `ng-uploader-options` to always check for obj presence * Marked `speedHumanized` and `speedAverageHumanized` as nullable * * Ignore WebStorm preferences * Fix and small improvement as discussed in PR comments. * Build fix + default value for withCredentials not being undefined. --- .gitignore | 1 + src/classes/ng-uploader-options.class.ts | 73 +++++++++++++----------- src/classes/uploaded-file.class.ts | 2 +- src/services/ngx-uploader.ts | 2 +- tsconfig.json | 1 + 5 files changed, 44 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index b566189e..415ede0c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ /typings !gulpfile.js +/.idea \ No newline at end of file diff --git a/src/classes/ng-uploader-options.class.ts b/src/classes/ng-uploader-options.class.ts index 6426de67..14da2507 100644 --- a/src/classes/ng-uploader-options.class.ts +++ b/src/classes/ng-uploader-options.class.ts @@ -1,3 +1,5 @@ +export type Method = 'POST' | 'GET' | 'PATCH' | 'PUT'; + export interface INgUploaderOptions { url: string; cors?: boolean; @@ -7,7 +9,7 @@ export interface INgUploaderOptions { data?: any; autoUpload?: boolean; multipart?: any; - method?: 'POST' | 'GET' | 'PATCH' | 'PUT'; + method?: Method; customHeaders?: any; encodeHeaders?: boolean; authTokenPrefix?: string; @@ -22,44 +24,49 @@ export interface INgUploaderOptions { export class NgUploaderOptions implements INgUploaderOptions { url: string; - cors?: boolean; - withCredentials?: boolean; - multiple?: boolean; + cors: boolean; + withCredentials: boolean; + multiple: boolean; maxUploads?: number; data?: any; - autoUpload?: boolean; + autoUpload: boolean; multipart?: any; - method?: 'POST' | 'GET' | 'PATCH' | 'PUT'; - customHeaders?: any; - encodeHeaders?: boolean; - authTokenPrefix?: string; + method: Method; + customHeaders: any; + encodeHeaders: boolean; + authTokenPrefix: string; authToken?: string; - fieldName?: string; - fieldReset?: boolean; + fieldName: string; + fieldReset: boolean; previewUrl?: string; - calculateSpeed?: boolean; - filterExtensions?: boolean; - allowedExtensions?: string[]; + calculateSpeed: boolean; + filterExtensions: boolean; + allowedExtensions: string[]; constructor(obj: INgUploaderOptions) { - this.url = obj.url != null ? obj.url : ''; - this.cors = obj.cors != null ? obj.cors : true; - this.withCredentials = obj.withCredentials != null ? obj.withCredentials : this.withCredentials; - this.multiple = obj.multiple != null ? obj.multiple : true; - this.maxUploads = obj.maxUploads != null ? obj.maxUploads : 10; - this.data = obj.data != null ? obj.data : {}; - this.autoUpload = obj.autoUpload != null ? obj.autoUpload : true; - this.multipart = obj.multipart != null ? obj.multipart : false; - this.method = obj.method != null ? obj.method : 'POST'; - this.customHeaders = obj.customHeaders != null ? obj.customHeaders : { }; - this.encodeHeaders = obj.encodeHeaders != null ? obj.encodeHeaders : false; - this.authTokenPrefix = obj.authTokenPrefix != null ? obj.authTokenPrefix : 'Bearer'; - this.authToken = obj.authToken != null ? obj.authToken : null; - this.fieldName = obj.fieldName != null ? obj.fieldName : 'file'; - this.fieldReset = obj.fieldReset != null ? obj.fieldReset : null; - this.previewUrl = obj.previewUrl != null ? obj.previewUrl : null; - this.calculateSpeed = obj.calculateSpeed != null ? obj.calculateSpeed : true; - this.filterExtensions = obj.filterExtensions != null ? obj.filterExtensions : false; - this.allowedExtensions = obj && obj.allowedExtensions ? obj.allowedExtensions : []; + function use(source: T|undefined, defaultValue: T): T { + return obj && source !== undefined ? source : defaultValue; + } + + this.url = use(obj.url, ''); + this.cors = use(obj.cors, true); + this.withCredentials = use(obj.withCredentials, false); + this.multiple = use(obj.multiple, true); + this.maxUploads = use(obj.maxUploads, 10); + this.data = use(obj.data, {}); + this.autoUpload = use(obj.autoUpload, true); + this.multipart = use(obj.multipart, false); + this.method = use(obj.method, 'POST'); + this.customHeaders = use(obj.customHeaders, {}); + this.encodeHeaders = use(obj.encodeHeaders, false); + this.authTokenPrefix = use(obj.authTokenPrefix, 'Bearer'); + this.authToken = use(obj.authToken, undefined); + this.fieldName = use(obj.fieldName, 'file'); + this.fieldReset = use(obj.fieldReset, false); + this.previewUrl = use(obj.previewUrl, undefined); + this.calculateSpeed = use(obj.calculateSpeed, true); + this.filterExtensions = use(obj.filterExtensions, false); + this.allowedExtensions = use(obj.allowedExtensions, []); } + } diff --git a/src/classes/uploaded-file.class.ts b/src/classes/uploaded-file.class.ts index b30014b3..e5ee4c6f 100644 --- a/src/classes/uploaded-file.class.ts +++ b/src/classes/uploaded-file.class.ts @@ -12,7 +12,7 @@ export class UploadedFile { startTime: number; endTime: number; speedAverage: number; - speedAverageHumanized: string; + speedAverageHumanized: string|null; constructor(id: string, originalName: string, size: number) { this.id = id; diff --git a/src/services/ngx-uploader.ts b/src/services/ngx-uploader.ts index c3de231d..a1262f3f 100644 --- a/src/services/ngx-uploader.ts +++ b/src/services/ngx-uploader.ts @@ -49,7 +49,7 @@ export class NgUploaderService { let time: number = new Date().getTime(); let load = 0; let speed = 0; - let speedHumanized: string = null; + let speedHumanized: string|null = null; xhr.upload.onprogress = (e: ProgressEvent) => { if (e.lengthComputable) { diff --git a/tsconfig.json b/tsconfig.json index 91664236..cab9b68e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "noEmitHelpers": false, "noImplicitAny": true, "declaration": true, + "strictNullChecks": true, "skipLibCheck": true, "stripInternal": true, "lib": ["es6", "dom"],