Skip to content

Commit

Permalink
Support for „strictNullChecks” in tsconfig (bleenco#170)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
krzysztof-miemiec authored and jkuri committed Jan 7, 2017
1 parent 47580c8 commit e1343c6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
/typings

!gulpfile.js
/.idea
73 changes: 40 additions & 33 deletions src/classes/ng-uploader-options.class.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Method = 'POST' | 'GET' | 'PATCH' | 'PUT';

export interface INgUploaderOptions {
url: string;
cors?: boolean;
Expand All @@ -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;
Expand All @@ -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<T>(source: T|undefined, defaultValue: T): T {
return obj && source !== undefined ? source : defaultValue;
}

this.url = use(obj.url, <string>'');
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, <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, []);
}

}
2 changes: 1 addition & 1 deletion src/classes/uploaded-file.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/services/ngx-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"noEmitHelpers": false,
"noImplicitAny": true,
"declaration": true,
"strictNullChecks": true,
"skipLibCheck": true,
"stripInternal": true,
"lib": ["es6", "dom"],
Expand Down

0 comments on commit e1343c6

Please sign in to comment.