Skip to content

Commit 51aad41

Browse files
committed
Added configuration option to turn off unmapped warnings, globally.
1 parent 787b200 commit 51aad41

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "simple-mapper",
33
"description": "Angular 2+ object-to-view-model mapper. Intended to facilitate working with JSON from web API endpoints.",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"license": "MIT",
66
"repository": "https://github.com/cdibbs/simple-mapper",
77
"scripts": {

src/app/services/i/i-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ export interface IConfig {
99

1010
/** Validate mapping configuration on startup. Default: false. */
1111
validateOnStartup?: boolean;
12+
13+
/** Turn off unmapped warnings globally. Can be overridden at the method level. */
14+
noUnmappedWarnings?: boolean;
1215
}

src/app/services/mapper.service.spec.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,9 @@ describe('MapperService', () => {
120120
Two: 6.53, // ignored
121121
};
122122
var result = mapper.MapJsonToVM(vm.MineMinusTwo, json, false);
123-
expect(result.Id).toBe(3);
124-
expect(result.One).toBe(json.One);
125-
expect(result["Two"]).toBeUndefined();
126123
expect(warned).toBeNull();
127124
}));
128-
it('should warn when extraneous source properties.',
125+
it('should warn when extraneous source properties, by default.',
129126
inject([MapperService], (mapper: MapperService) => {
130127
var warned = null;
131128
mapper["log"].warn = function(yep) { warned = yep; };
@@ -135,9 +132,45 @@ describe('MapperService', () => {
135132
Two: 6.53, // ignored
136133
};
137134
var result = mapper.MapJsonToVM(vm.MineMinusTwo, json);
138-
expect(result.Id).toBe(3);
139-
expect(result.One).toBe(json.One);
140-
expect(result["Two"]).toBeUndefined();
135+
expect(warned).toBeTruthy();
136+
}));
137+
it('should warn when extraneous source properties when global=off, method=on (overrides global).',
138+
inject([MapperService], (mapper: MapperService) => {
139+
var warned = null;
140+
mapper["noUnmappedWarnings"] = true;
141+
mapper["log"].warn = function(yep) { warned = yep; };
142+
var json = {
143+
Id: 3,
144+
One: "something else",
145+
Two: 6.53, // ignored
146+
};
147+
var result = mapper.MapJsonToVM(vm.MineMinusTwo, json, true);
148+
expect(warned).toBeTruthy();
149+
}));
150+
it('should not warn when extraneous source properties when global=on, method=off (overrides global).',
151+
inject([MapperService], (mapper: MapperService) => {
152+
var warned = null;
153+
mapper["noUnmappedWarnings"] = false;
154+
mapper["log"].warn = function(yep) { warned = yep; };
155+
var json = {
156+
Id: 3,
157+
One: "something else",
158+
Two: 6.53, // ignored
159+
};
160+
var result = mapper.MapJsonToVM(vm.MineMinusTwo, json, false);
161+
expect(warned).toBeFalsy();
162+
}));
163+
it('should warn when extraneous source properties when global=on, method=on (overrides global).',
164+
inject([MapperService], (mapper: MapperService) => {
165+
var warned = null;
166+
mapper["noUnmappedWarnings"] = false;
167+
mapper["log"].warn = function(yep) { warned = yep; };
168+
var json = {
169+
Id: 3,
170+
One: "something else",
171+
Two: 6.53, // ignored
172+
};
173+
var result = mapper.MapJsonToVM(vm.MineMinusTwo, json, true);
141174
expect(warned).toBeTruthy();
142175
}));
143176
it('should not warn when not extraneous source properties.',

src/app/services/mapper.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ export let MapperConfiguration = new InjectionToken<IConfig>("MapperConfiguratio
1111
export class MapperService implements IMapperService {
1212
private viewModels: { [key: string]: any };
1313
private log: ILogService;
14+
private noUnmappedWarnings: boolean;
1415

1516
public constructor(@Inject(MapperConfiguration) private config: IConfig)
1617
{
1718
this.viewModels = config.viewModels || {};
1819
this.log = config.logger || console;
20+
this.noUnmappedWarnings = !!config.noUnmappedWarnings;
1921
if (config.validateOnStartup) {
2022
this.validate();
2123
}
@@ -30,12 +32,12 @@ export class MapperService implements IMapperService {
3032
* @return {T} The constructed view model with all of its properties mapped from the source json.
3133
* @example MapJsonToVM(UserViewModel, userJson);
3234
*/
33-
public MapJsonToVM<T extends { [key: string]: any }>(t: { new (): T }, json: any, unmappedWarning = true): T {
35+
public MapJsonToVM<T extends { [key: string]: any }>(t: { new (): T }, json: any, unmappedWarning = undefined): T {
3436
let vm = new t();
3537
let tprops = getMappableProperties(vm);
3638
let keys = Object.keys(json || {});
3739

38-
if (unmappedWarning) {
40+
if (unmappedWarning === true || (unmappedWarning === undefined && ! this.noUnmappedWarnings)) {
3941
let t2props = Object.keys(vm);
4042
let unmapped = keys.filter(k => Object.keys(tprops).indexOf(k) < 0 && t2props.indexOf(k) < 0);
4143
if (unmapped.length)

0 commit comments

Comments
 (0)