Skip to content

Commit eb6f98f

Browse files
author
Anatoly Ostrovsky
committed
New validation func
1 parent 6a0a478 commit eb6f98f

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
make lint
12
make check
23
make types
34
make pretty

@types/shared/utils.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ export function shallowCopy(src: any, dst: any): any;
433433
* @param {string} errorMsg
434434
*/
435435
export function assert(argument: boolean, errorMsg?: string): void;
436+
/**
437+
* Throw error if the argument is falsy.
438+
*/
439+
export function validate(fn: any, arg: any, name: any): any;
436440
/**
437441
* Throw error if the argument is falsy.
438442
*/

docs/static/examples/i18n/i18n.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
i18next.changeLanguage(y);
5959
return i18next.t(x);
6060
};
61+
})
62+
.filter('translatesimple', ($rootScope) => {
63+
return (x) => {
64+
i18next.changeLanguage($rootScope.lang);
65+
return i18next.t(x);
66+
};
6167
});
6268
});
6369
</script>
@@ -68,10 +74,12 @@
6874
</style>
6975
</head>
7076
<body ng-app="i18n" ng-init="lang = 'en'">
77+
<div>Current lang: {{ lang }}</div>
7178
<input type="radio" ng-model="lang" value="en" /> English
72-
<input type="radio" ng-model="lang" value="ru" /> Russian {{ lang }} {{
73-
'greeting' | translate:lang }}
79+
<input type="radio" ng-model="lang" value="ru" /> Russian
7480

81+
<div>{{ 'greeting' | translate:lang }}</div>
82+
<div>{{ 'greeting' | translatesimple }}</div>
7583
<div ng-translate class="capitalize">Hello world</div>
7684
</body>
7785
</html>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { $injectTokens as $t } from "../../../injection-tokens.js";
22
import { createWorkerConnection } from "../../../directive/worker/worker.js";
33
import {
4-
BADARG,
5-
assert,
64
instantiateWasm,
75
isFunction,
86
isString,
7+
assertArg,
8+
validate,
99
} from "../../../shared/utils.js";
1010

1111
/**
@@ -21,8 +21,8 @@ export class NgModule {
2121
* @param {ng.Injectable<any>} [configFn]
2222
*/
2323
constructor(name, requires, configFn) {
24-
assert(isString(name), "name required");
25-
assert(Array.isArray(requires), "requires array required");
24+
validate(isString, name, "name");
25+
validate(Array.isArray, requires, "not array");
2626
/**
2727
* Name of the current module.
2828
* @type {string}
@@ -197,8 +197,8 @@ export class NgModule {
197197
* @return {NgModule}
198198
*/
199199
filter(name, filterFn) {
200-
assert(isString(name), `${BADARG}:name ${name}`);
201-
assert(isFunction(filterFn), `${BADARG}:filter ${filterFn}`);
200+
assertArg(isString(name), "name");
201+
assertArg(isFunction(filterFn), `filterFn`);
202202
this.invokeQueue.push([$t.$filterProvider, "register", [name, filterFn]]);
203203

204204
return this;

src/shared/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,28 @@ export function assert(argument, errorMsg = "Assertion failed") {
949949
}
950950
}
951951

952+
function getReason(val) {
953+
switch (val) {
954+
case isNullOrUndefined:
955+
return "required";
956+
case Array.isArray:
957+
return "notarray";
958+
default:
959+
return "fail";
960+
}
961+
}
962+
963+
/**
964+
* Throw error if the argument is falsy.
965+
*/
966+
export function validate(fn, arg, name) {
967+
if (!fn(arg)) {
968+
throw new TypeError(`badarg:${getReason(fn)} '${name}'=${arg}`);
969+
}
970+
971+
return arg;
972+
}
973+
952974
/**
953975
* Throw error if the argument is falsy.
954976
*/

0 commit comments

Comments
 (0)