Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Unified Diff: third_party/pkg/angular/lib/utils.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/pkg/angular/lib/utils.dart
diff --git a/third_party/pkg/angular/lib/utils.dart b/third_party/pkg/angular/lib/utils.dart
new file mode 100644
index 0000000000000000000000000000000000000000..9f0324747d4162ac9dca3ce8d372cfe0d5f72e26
--- /dev/null
+++ b/third_party/pkg/angular/lib/utils.dart
@@ -0,0 +1,88 @@
+library angular.util;
+
+toBool(x) {
+ if (x is bool) return x;
+ if (x is num) return x != 0;
+ return false;
+}
+
+typedef FnWith0Args();
+typedef FnWith1Args(a0);
+typedef FnWith2Args(a0, a1);
+typedef FnWith3Args(a0, a1, a2);
+typedef FnWith4Args(a0, a1, a2, a3);
+typedef FnWith5Args(a0, a1, a2, a3, a4);
+
+relaxFnApply(Function fn, List args) {
+// Check the args.length to support functions with optional parameters.
+ var argsLen = args.length;
+ if (fn is Function && fn != null) {
+ if (fn is FnWith5Args && argsLen > 4) {
+ return fn(args[0], args[1], args[2], args[3], args[4]);
+ } else if (fn is FnWith4Args && argsLen > 3) {
+ return fn(args[0], args[1], args[2], args[3]);
+ } else if (fn is FnWith3Args && argsLen > 2 ) {
+ return fn(args[0], args[1], args[2]);
+ } else if (fn is FnWith2Args && argsLen > 1 ) {
+ return fn(args[0], args[1]);
+ } else if (fn is FnWith1Args && argsLen > 0) {
+ return fn(args[0]);
+ } else if (fn is FnWith0Args) {
+ return fn();
+ } else {
+ throw "Unknown function type, expecting 0 to 5 args.";
+ }
+ } else {
+ throw "Missing function.";
+ }
+}
+
+relaxFnArgs1(Function fn) {
+ if (fn is FnWith3Args) return (_1) => fn(_1, null, null);
+ if (fn is FnWith2Args) return (_1) => fn(_1, null);
+ if (fn is FnWith1Args) return fn;
+ if (fn is FnWith0Args) return (_1) => fn();
+}
+
+relaxFnArgs3(Function fn) {
+ if (fn is FnWith3Args) return fn;
+ if (fn is FnWith2Args) return (_1, _2, _3) => fn(_1, null);
+ if (fn is FnWith1Args) return (_1, _2, _3) => fn(_1);
+ if (fn is FnWith0Args) return (_1, _2, _3) => fn();
+}
+
+relaxFnArgs(Function fn) {
+ if (fn is FnWith5Args) {
+ return ([a0, a1, a2, a3, a4]) => fn(a0, a1, a2, a3, a4);
+ } else if (fn is FnWith4Args) {
+ return ([a0, a1, a2, a3, a4]) => fn(a0, a1, a2, a3);
+ } else if (fn is FnWith3Args) {
+ return ([a0, a1, a2, a3, a4]) => fn(a0, a1, a2);
+ } else if (fn is FnWith2Args) {
+ return ([a0, a1, a2, a3, a4]) => fn(a0, a1);
+ } else if (fn is FnWith1Args) {
+ return ([a0, a1, a2, a3, a4]) => fn(a0);
+ } else if (fn is FnWith0Args) {
+ return ([a0, a1, a2, a3, a4]) => fn();
+ } else {
+ return ([a0, a1, a2, a3, a4]) {
+ throw "Unknown function type, expecting 0 to 5 args.";
+ };
+ }
+}
+
+camelcase(String s) {
+ var part = s.split('-').map((s) => s.toLowerCase());
+ if (part.length <= 1) {
+ return part.join();
+ }
+ return part.first + part.skip(1).map(capitalize).join();
+}
+
+capitalize(String s) => s.substring(0, 1).toUpperCase() + s.substring(1);
+
+var SNAKE_CASE_REGEXP = new RegExp("[A-Z]");
+
+snakecase(String name, [separator = '-']) =>
+ name.replaceAllMapped(SNAKE_CASE_REGEXP, (Match match) =>
+ (match.start != 0 ? separator : '') + match.group(0).toLowerCase());

Powered by Google App Engine
This is Rietveld 408576698