OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library utils; | 5 library utils; |
6 import 'package:web_components/src/template/world.dart'; | 6 import 'package:web_components/src/template/world.dart'; |
7 | 7 |
8 /** | 8 /** |
9 * Converts a string name with hyphens into an identifier, by removing hyphens | 9 * Converts a string name with hyphens into an identifier, by removing hyphens |
10 * and capitalizing the following letter. | 10 * and capitalizing the following letter. |
11 */ | 11 */ |
12 String toCamelCase(String hyphenedName) { | 12 String toCamelCase(String hyphenedName) { |
13 var segments = hyphenedName.split('-'); | 13 var segments = hyphenedName.split('-'); |
14 for (int i = 1; i < segments.length; i++) { | 14 for (int i = 1; i < segments.length; i++) { |
15 var segment = segments[i]; | 15 var segment = segments[i]; |
16 if (segment.length > 0) { | 16 if (segment.length > 0) { |
17 // Character between 'a'..'z' mapped to 'A'..'Z' | 17 // Character between 'a'..'z' mapped to 'A'..'Z' |
18 segments[i] = '${segment[0].toUpperCase()}${segment.substring(1)}'; | 18 segments[i] = '${segment[0].toUpperCase()}${segment.substring(1)}'; |
19 } | 19 } |
20 } | 20 } |
21 return Strings.join(segments, ''); | 21 return Strings.join(segments, ''); |
22 } | 22 } |
23 | 23 |
24 /** | 24 /** |
25 * Invokes [callback], logs how long it took to execute in ms, and returns | 25 * Invokes [callback], logs how long it took to execute in ms, and returns |
26 * whatever [callback] returns. The log message will be printed if either | 26 * whatever [callback] returns. The log message will be printed if either |
27 * [:options.showInfo:] or [printTime] are true. | 27 * [:options.showInfo:] or [printTime] are true. |
28 */ | 28 */ |
29 time(String logMessage, callback(), [bool printTime = false]) { | 29 time(String logMessage, callback(), {bool printTime: false}) { |
30 final watch = new Stopwatch(); | 30 final watch = new Stopwatch(); |
31 watch.start(); | 31 watch.start(); |
32 var result = callback(); | 32 var result = callback(); |
33 watch.stop(); | 33 watch.stop(); |
34 final duration = watch.elapsedInMs(); | 34 final duration = watch.elapsedInMs(); |
35 if (options.showInfo || printTime) { | 35 if (options.showInfo || printTime) { |
36 print('$logMessage in $GREEN_COLOR$duration ms$NO_COLOR'); | 36 print('$logMessage in $GREEN_COLOR$duration ms$NO_COLOR'); |
37 } | 37 } |
38 return result; | 38 return result; |
39 } | 39 } |
40 | 40 |
| 41 /** |
| 42 * Invokes [callback], logs how long it takes from the moment [callback] is |
| 43 * executed until the future it returns is completed. Returns the future |
| 44 * returned by [callback]. The log message will be printed if either |
| 45 * [:options.showInfo:] or [printTime] are true. |
| 46 */ |
| 47 Future asyncTime(String logMessage, Future callback(), |
| 48 {bool printTime: false}) { |
| 49 final watch = new Stopwatch(); |
| 50 watch.start(); |
| 51 return callback()..then((_) { |
| 52 watch.stop(); |
| 53 final duration = watch.elapsedInMs(); |
| 54 if (options.showInfo || printTime) { |
| 55 print('$logMessage in $GREEN_COLOR$duration ms$NO_COLOR'); |
| 56 } |
| 57 }); |
| 58 } |
| 59 |
41 // Color constants used for generating messages. | 60 // Color constants used for generating messages. |
42 final String GREEN_COLOR = '\u001b[32m'; | 61 final String GREEN_COLOR = '\u001b[32m'; |
43 final String RED_COLOR = '\u001b[31m'; | 62 final String RED_COLOR = '\u001b[31m'; |
44 final String MAGENTA_COLOR = '\u001b[35m'; | 63 final String MAGENTA_COLOR = '\u001b[35m'; |
45 final String NO_COLOR = '\u001b[0m'; | 64 final String NO_COLOR = '\u001b[0m'; |
46 | 65 |
47 /** Find and return the first element in [list] that satisfies [matcher]. */ | 66 /** Find and return the first element in [list] that satisfies [matcher]. */ |
48 find(List list, bool matcher(elem)) { | 67 find(List list, bool matcher(elem)) { |
49 for (var elem in list) { | 68 for (var elem in list) { |
50 if (matcher(elem)) return elem; | 69 if (matcher(elem)) return elem; |
51 } | 70 } |
52 return null; | 71 return null; |
53 } | 72 } |
OLD | NEW |