Skip to content

Commit 5648bc2

Browse files
committed
Remove circular dependency
1 parent 924ebaf commit 5648bc2

File tree

10 files changed

+130
-138
lines changed

10 files changed

+130
-138
lines changed

@types/shared/utils.d.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -431,39 +431,6 @@ export function shallowCopy(src: any, dst: any): any;
431431
* @param {string} errorMsg
432432
*/
433433
export function assert(argument: boolean, errorMsg?: string): void;
434-
/**
435-
* Validate a value using a predicate function.
436-
* Throws if the predicate returns false.
437-
* IMPORTANT: use this function only for developper errors and not for user/data errors
438-
*
439-
* @param {ng.Validator} fn - Predicate validator function.
440-
* @param {*} arg - The value to validate.
441-
* @param {string} name - Parameter name (included in error message).
442-
* @returns {*} The validated value.
443-
* @throws {TypeError} If the value does not satisfy the validator.
444-
*/
445-
export function validate(fn: ng.Validator, arg: any, name: string): any;
446-
/**
447-
* @param {*} arg - The value to validate.
448-
* @param {string} name - Parameter name (included in error message).
449-
* @returns {*} The validated value.
450-
* @throws {TypeError} If the value does not satisfy the validator.
451-
*/
452-
export function validateRequired(arg: any, name: string): any;
453-
/**
454-
* @param {*} arg - The value to validate.
455-
* @param {string} name - Parameter name (included in error message).
456-
* @returns {*} The validated value.
457-
* @throws {TypeError} If the value does not satisfy the validator.
458-
*/
459-
export function validateArray(arg: any, name: string): any;
460-
/**
461-
* @param {*} arg - The value to validate.
462-
* @param {string} name - Parameter name (included in error message).
463-
* @returns {*} The validated value.
464-
* @throws {TypeError} If the value does not satisfy the validator.
465-
*/
466-
export function validateIsString(arg: any, name: string): any;
467434
/**
468435
* Throw error if the argument is falsy.
469436
*/
@@ -637,7 +604,4 @@ export function instantiateWasm(
637604
*/
638605
export function isArrowFunction(fn: any): boolean;
639606
export const isProxySymbol: unique symbol;
640-
export const BADARG: "badarg";
641-
export const BADARGKEY: "badarg: key";
642-
export const BADARGVALUE: "badarg: value";
643607
export const ngAttrPrefixes: string[];

@types/shared/validate.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Validate a value using a predicate function.
3+
* Throws if the predicate returns false.
4+
* IMPORTANT: use this function only for developper errors and not for user/data errors
5+
*
6+
* @param {ng.Validator} fn - Predicate validator function.
7+
* @param {*} arg - The value to validate.
8+
* @param {string} name - Parameter name (included in error message).
9+
* @returns {*} The validated value.
10+
* @throws {TypeError} If the value does not satisfy the validator.
11+
*/
12+
export function validate(fn: ng.Validator, arg: any, name: string): any;
13+
/**
14+
* @param {*} arg - The value to validate.
15+
* @param {string} name - Parameter name (included in error message).
16+
* @returns {*} The validated value.
17+
* @throws {TypeError} If the value does not satisfy the validator.
18+
*/
19+
export function validateRequired(arg: any, name: string): any;
20+
/**
21+
* @param {*} arg - The value to validate.
22+
* @param {string} name - Parameter name (included in error message).
23+
* @returns {*} The validated value.
24+
* @throws {TypeError} If the value does not satisfy the validator.
25+
*/
26+
export function validateArray(arg: any, name: string): any;
27+
/**
28+
* @param {*} arg - The value to validate.
29+
* @param {string} name - Parameter name (included in error message).
30+
* @returns {*} The validated value.
31+
* @throws {TypeError} If the value does not satisfy the validator.
32+
*/
33+
export function validateIsString(arg: any, name: string): any;
34+
export const BADARG: "badarg";
35+
export const BADARGKEY: "badarg: key";
36+
export const BADARGVALUE: "badarg: value";

src/core/di/injector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
isString,
99
isUndefined,
1010
minErr,
11-
validateArray,
1211
} from "../../shared/utils.js";
1312
import { InjectorService, ProviderInjector } from "./internal-injector.js";
1413
import { createPersistentProxy } from "../../services/storage/storage.js";
15-
import { $injectTokens } from "../../injection-tokens";
14+
import { $injectTokens } from "../../injection-tokens.js";
15+
import { validateArray } from "../../shared/validate.js";
1616

1717
const $injectorMinErr = minErr($injectTokens.$injector);
1818

src/core/di/ng-module/ng-module.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import {
44
instantiateWasm,
55
isFunction,
66
isString,
7-
validate,
87
isDefined,
9-
validateRequired,
108
isObject,
119
} from "../../../shared/utils.js";
1210
import { isInjectable } from "../../../shared/predicates.js";
11+
import { validate, validateRequired } from "../../../shared/validate.js";
1312

1413
/**
1514
* Modules are collections of application configuration information for components:

src/core/filter/filter.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ import {
77
$IncludedByStateFilter,
88
$IsStateFilter,
99
} from "../../router/state-filters.js";
10-
import {
11-
BADARG,
12-
assert,
13-
isDefined,
14-
isFunction,
15-
isString,
16-
} from "../../shared/utils.js";
10+
import { assert, isDefined, isFunction } from "../../shared/utils.js";
11+
import { validate, validateIsString } from "../../shared/validate.js";
1712

1813
/* @ignore */
1914
const SUFFIX = "Filter";
@@ -52,8 +47,8 @@ export class FilterProvider {
5247
* @return {ng.FilterProvider}
5348
*/
5449
register(name, factory) {
55-
assert(isString(name), `${BADARG}:name ${name}`);
56-
assert(isFunction(factory), `${BADARG}:factory ${factory}`);
50+
validateIsString(name, "name");
51+
validate(isFunction, factory, "factory");
5752
this.$provide.factory(name + SUFFIX, factory);
5853

5954
return this;
@@ -66,7 +61,7 @@ export class FilterProvider {
6661
* @returns {ng.FilterService}
6762
*/
6863
($injector) => (name) => {
69-
assert(isString(name), `${BADARG}:name ${name}`);
64+
validateIsString(name, "name");
7065

7166
return $injector.get(name + SUFFIX);
7267
},

src/directive/options/options.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
isArrayLike,
88
isDefined,
99
minErr,
10-
validateRequired,
1110
} from "../../shared/utils.js";
1211

1312
const ngOptionsMinErr = minErr("ngOptions");
@@ -420,7 +419,6 @@ export function ngOptionsDirective($compile, $parse) {
420419
// compile the element since there might be bindings in it
421420
const linkFn = $compile(selectCtrl.emptyOption);
422421

423-
validateRequired(linkFn, "linkFn");
424422
selectElement.prepend(selectCtrl.emptyOption);
425423
linkFn(scope);
426424

src/services/cookie/cookie.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { $injectTokens } from "../../injection-tokens.js";
22
import {
3-
BADARG,
4-
BADARGVALUE,
53
assert,
64
isDefined,
75
isNullOrUndefined,
86
isNumber,
97
isString,
8+
} from "../../shared/utils.js";
9+
import {
1010
validateIsString,
1111
validateRequired,
12-
} from "../../shared/utils.js";
12+
BADARGVALUE,
13+
BADARG,
14+
} from "../../shared/validate.js";
1315

1416
/**
1517
* Service provider that creates a {@link CookieService $cookie} service.

src/services/rest/rest.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { $injectTokens } from "../../injection-tokens.js";
2-
import {
3-
assert,
4-
BADARG,
5-
isNullOrUndefined,
6-
isString,
7-
} from "../../shared/utils.js";
2+
import { assert, isNullOrUndefined, isString } from "../../shared/utils.js";
3+
import { BADARG } from "../../shared/validate.js";
84
import { expandUriTemplate } from "./rfc.js";
95

106
/**

src/shared/utils.js

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { PREFIX_REGEXP, SPECIAL_CHARS_REGEXP } from "./constants.js";
2-
import { isInjectable } from "./predicates";
32

43
export const isProxySymbol = Symbol("isProxy");
5-
export const BADARG = "badarg";
6-
export const BADARGKEY = "badarg: key";
7-
export const BADARGVALUE = "badarg: value";
84

95
/**
106
*
@@ -956,79 +952,6 @@ export function assert(argument, errorMsg = "Assertion failed") {
956952
}
957953
}
958954

959-
/** @type {Map<ng.Validator, string>} */
960-
const reasons = new Map([
961-
[notNullOrUndefined, "required"],
962-
[Array.isArray, "notarray"],
963-
[isInjectable, "notinjectable"],
964-
[isDefined, "required"],
965-
[isString, "notstring"],
966-
]);
967-
968-
/**
969-
*
970-
* @param {ng.Validator} val
971-
* @returns {string}
972-
*/
973-
function getReason(val) {
974-
return reasons.get(val) ?? "fail";
975-
}
976-
977-
/**
978-
* Validate a value using a predicate function.
979-
* Throws if the predicate returns false.
980-
* IMPORTANT: use this function only for developper errors and not for user/data errors
981-
*
982-
* @param {ng.Validator} fn - Predicate validator function.
983-
* @param {*} arg - The value to validate.
984-
* @param {string} name - Parameter name (included in error message).
985-
* @returns {*} The validated value.
986-
* @throws {TypeError} If the value does not satisfy the validator.
987-
*/
988-
export function validate(fn, arg, name) {
989-
if (fn(arg)) return arg;
990-
991-
let v;
992-
993-
try {
994-
v = JSON.stringify(arg);
995-
} catch {
996-
v = String(arg);
997-
}
998-
999-
throw new TypeError(`badarg:${getReason(fn)} ${name}=${v}`);
1000-
}
1001-
1002-
/**
1003-
* @param {*} arg - The value to validate.
1004-
* @param {string} name - Parameter name (included in error message).
1005-
* @returns {*} The validated value.
1006-
* @throws {TypeError} If the value does not satisfy the validator.
1007-
*/
1008-
export function validateRequired(arg, name) {
1009-
return validate(notNullOrUndefined, arg, name);
1010-
}
1011-
1012-
/**
1013-
* @param {*} arg - The value to validate.
1014-
* @param {string} name - Parameter name (included in error message).
1015-
* @returns {*} The validated value.
1016-
* @throws {TypeError} If the value does not satisfy the validator.
1017-
*/
1018-
export function validateArray(arg, name) {
1019-
return validate(Array.isArray, arg, name);
1020-
}
1021-
1022-
/**
1023-
* @param {*} arg - The value to validate.
1024-
* @param {string} name - Parameter name (included in error message).
1025-
* @returns {*} The validated value.
1026-
* @throws {TypeError} If the value does not satisfy the validator.
1027-
*/
1028-
export function validateIsString(arg, name) {
1029-
return validate(isString, arg, name);
1030-
}
1031-
1032955
/**
1033956
* Throw error if the argument is falsy.
1034957
*/

src/shared/validate.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { isInjectable } from "./predicates.js";
2+
import { isDefined, isString, notNullOrUndefined } from "./utils.js";
3+
4+
export const BADARG = "badarg";
5+
export const BADARGKEY = "badarg: key";
6+
export const BADARGVALUE = "badarg: value";
7+
8+
/** @type {Map<ng.Validator, string>} */
9+
const reasons = new Map([
10+
[notNullOrUndefined, "required"],
11+
[Array.isArray, "notarray"],
12+
[isInjectable, "notinjectable"],
13+
[isDefined, "required"],
14+
[isString, "notstring"],
15+
]);
16+
17+
/**
18+
*
19+
* @param {ng.Validator} val
20+
* @returns {string}
21+
*/
22+
function getReason(val) {
23+
return reasons.get(val) ?? "fail";
24+
}
25+
26+
/**
27+
* Validate a value using a predicate function.
28+
* Throws if the predicate returns false.
29+
* IMPORTANT: use this function only for developper errors and not for user/data errors
30+
*
31+
* @param {ng.Validator} fn - Predicate validator function.
32+
* @param {*} arg - The value to validate.
33+
* @param {string} name - Parameter name (included in error message).
34+
* @returns {*} The validated value.
35+
* @throws {TypeError} If the value does not satisfy the validator.
36+
*/
37+
export function validate(fn, arg, name) {
38+
if (fn(arg)) return arg;
39+
40+
let v;
41+
42+
try {
43+
v = JSON.stringify(arg);
44+
} catch {
45+
v = String(arg);
46+
}
47+
48+
throw new TypeError(`badarg:${getReason(fn)} ${name}=${v}`);
49+
}
50+
51+
/**
52+
* @param {*} arg - The value to validate.
53+
* @param {string} name - Parameter name (included in error message).
54+
* @returns {*} The validated value.
55+
* @throws {TypeError} If the value does not satisfy the validator.
56+
*/
57+
export function validateRequired(arg, name) {
58+
return validate(notNullOrUndefined, arg, name);
59+
}
60+
61+
/**
62+
* @param {*} arg - The value to validate.
63+
* @param {string} name - Parameter name (included in error message).
64+
* @returns {*} The validated value.
65+
* @throws {TypeError} If the value does not satisfy the validator.
66+
*/
67+
export function validateArray(arg, name) {
68+
return validate(Array.isArray, arg, name);
69+
}
70+
71+
/**
72+
* @param {*} arg - The value to validate.
73+
* @param {string} name - Parameter name (included in error message).
74+
* @returns {*} The validated value.
75+
* @throws {TypeError} If the value does not satisfy the validator.
76+
*/
77+
export function validateIsString(arg, name) {
78+
return validate(isString, arg, name);
79+
}

0 commit comments

Comments
 (0)