Skip to content

Commit 454342c

Browse files
committed
feat(facade/collections): add getValueFromPath and baseGet to StringMapWrapper
- these allows us to get nested values from nested Objects
1 parent b54c462 commit 454342c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/facade/collections.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export class StringMapWrapper {
146146

147147
static keys( map: {[key: string]: any} ): string[] { return Object.keys( map ); }
148148

149+
static size( map: {[key: string]: any} ): number { return StringMapWrapper.keys( map ).length; }
150+
149151
static isEmpty( map: {[key: string]: any} ): boolean {
150152
for ( var prop in map ) {
151153
return false;

src/facade/lang.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function isDate( obj ): boolean {
8080
return obj instanceof Date && !isNaN( obj.valueOf() );
8181
}
8282

83-
export function isType( obj: any ): boolean {
83+
export function isType( obj: any ): obj is Type {
8484
return isFunction( obj );
8585
}
8686

@@ -229,6 +229,42 @@ function _firstTo( value: string, cb: Function ): string {
229229
return cb.call( value.charAt( 0 ) ) + value.substring( 1 );
230230
}
231231

232+
export function normalizeBlank(obj: Object): any {
233+
return isBlank(obj) ? null : obj;
234+
}
235+
236+
export function normalizeBool(obj: boolean): boolean {
237+
return isBlank(obj) ? false : obj;
238+
}
239+
240+
export function print(obj: Error | Object) {
241+
console.log(obj);
242+
}
243+
244+
/**
245+
* Angular 2 setValueOnPath
246+
* supports only `.` path separator
247+
* @param global
248+
* @param path
249+
* @param value
250+
*/
251+
export function setValueOnPath(global: any, path: string, value: any) {
252+
var parts = path.split('.');
253+
var obj: any = global;
254+
while (parts.length > 1) {
255+
var name = parts.shift();
256+
if (obj.hasOwnProperty(name) && isPresent(obj[name])) {
257+
obj = obj[name];
258+
} else {
259+
obj = obj[name] = {};
260+
}
261+
}
262+
if (obj === undefined || obj === null) {
263+
obj = {};
264+
}
265+
obj[parts.shift()] = value;
266+
}
267+
232268
/**
233269
* Converts `value` to an object if it's not one.
234270
*

0 commit comments

Comments
 (0)