1 | import { warning } from './log';
|
2 | import { typeOf } from './object';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export function camelcase(str) {
|
10 | if (!/-/.test(str)) {
|
11 | return str || '';
|
12 | }
|
13 | return str.toLowerCase().replace(/-([a-z])/g, function ($0, $1) {
|
14 | return $1.toUpperCase();
|
15 | });
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export function hyphenate(str) {
|
24 | var strType = typeOf(str);
|
25 | if (strType !== 'String') {
|
26 | warning('[ hyphenate(str: string): string ] ' + ('Expected arguments[0] to be a string but get a ' + strType + '.') + 'It will return an empty string without any processing.');
|
27 | return '';
|
28 | }
|
29 | return str.replace(/([A-Z])/g, function ($0) {
|
30 | return '-' + $0.toLowerCase();
|
31 | });
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export function template(tpl) {
|
41 | var object = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
42 |
|
43 | var tplType = typeOf(tpl);
|
44 | if (tplType !== 'String') {
|
45 | warning('[ template(tpl: string, object: object): string ] ' + ('Expected arguments[0] to be a string but get a ' + tplType + '.') + 'It will return an empty string without any processing.');
|
46 | return '';
|
47 | }
|
48 |
|
49 | return tpl.replace(/\{[a-z]*\}/g, function (str) {
|
50 | var key = str.substring(1, str.length - 1);
|
51 | return object[key] || '';
|
52 | });
|
53 | } |
\ | No newline at end of file |