Skip to content

Commit 08f9bb9

Browse files
committed
add template fn
1 parent c670d28 commit 08f9bb9

File tree

11 files changed

+574
-17
lines changed

11 files changed

+574
-17
lines changed

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ interface Traverse<T> {
3636
filter(pred: (node: T) => boolean): T[];
3737
}
3838
export declare function traverse<T>(tree: T & { children: T[] }): Traverse<T & { children: T[] }>;
39+
40+
/* @file ./src/template.js */
41+
export declare function template<T>(template: string, context: T): any;

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
/**
2+
*
3+
* Copyright 2019 Rightech IoT. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
117

218

319
module.exports = Object.assign({}, {
420
node: require('./src/node'),
521
safe: require('./src/safe'),
22+
format: require('./src/format'),
623
traverse: require('./src/traverse'),
24+
template: require('./src/template'),
725
log: require('./src/log')
826
});

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rightech/utils",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "",
55
"main": "index.js",
66
"private": false,
@@ -18,8 +18,11 @@
1818
},
1919
"homepage": "https://github.com/Rightech/node-utils#readme",
2020
"dependencies": {
21-
"colors": "^1.3.3",
22-
"deepmerge": "^4.0.0",
21+
"angular-expressions": "^1.0.0",
22+
"colors": "^1.4.0",
23+
"deepmerge": "^4.2.2",
24+
"mustache": "^3.1.0",
25+
"nanoid": "^2.1.6",
2326
"stack-trace": "0.0.10"
2427
},
2528
"typings": "index.d.ts"

src/format.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
*
3+
* Copyright 2019 Rightech IoT. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const { startOfDay, timeOf, getLocalTime, getOffset } = require('./time');
19+
20+
const DEFAULT_FALLBACK = '-';
21+
const DEFAULT_LOCALE = 'en';
22+
const DEFAULT_24H_LOCALE = 'en-GB';
23+
24+
25+
let fallback = DEFAULT_FALLBACK;
26+
let currentLocale = DEFAULT_LOCALE;
27+
28+
const intlCache = {};
29+
30+
function getIntl(type, key, locale, options) {
31+
if (!locale) {
32+
locale = currentLocale;
33+
}
34+
key = `${type}.${key}.${locale}`;
35+
if (key in intlCache) {
36+
return intlCache[key];
37+
}
38+
return intlCache[key] = options
39+
? new Intl[type](locale, options)
40+
: new Intl[type](locale);
41+
}
42+
43+
function setLocale(locale) {
44+
currentLocale = locale;
45+
return { locale, currentLocale };
46+
}
47+
48+
function date(value, locale) {
49+
if (typeof value === 'string') {
50+
value = +value;
51+
}
52+
if (!value || isNaN(value)) {
53+
return fallback;
54+
}
55+
return getIntl('DateTimeFormat', 'date', locale)
56+
.format(getLocalTime(value));
57+
}
58+
59+
function time(value, locale) {
60+
if (typeof value === 'string') {
61+
value = +value;
62+
}
63+
if (!value || isNaN(value)) {
64+
return fallback;
65+
}
66+
const options = {
67+
hour: 'numeric',
68+
minute: 'numeric',
69+
second: 'numeric'
70+
};
71+
return getIntl('DateTimeFormat', 'time', locale, options)
72+
.format(getLocalTime(value));
73+
}
74+
75+
function dateTime(value, locale) {
76+
if (!value || isNaN(value)) {
77+
return fallback;
78+
}
79+
return `${date(value, locale)} ${time(value, locale)}`;
80+
}
81+
82+
function dateOrTime(value, from, locale) {
83+
if (!value || isNaN(value)) {
84+
return fallback;
85+
}
86+
let local = getLocalTime(value || Date.now());
87+
return local >= startOfDay(from)
88+
? time(value, locale)
89+
: date(value, locale);
90+
}
91+
92+
function timeSpan(date) {
93+
if (typeof date === 'undefined' || date === 0) { // eslint-disable-line no-magic-numbers
94+
return fallback;
95+
}
96+
let days = Math.floor(+date / timeOf(1).days); // eslint-disable-line no-magic-numbers
97+
days = days > 0 ? (`${days}:`) : ''; // eslint-disable-line no-magic-numbers
98+
date = (+date + getOffset() * 60 * 1000);
99+
return days + time(date, DEFAULT_24H_LOCALE);
100+
}
101+
102+
103+
function number(value, fixed) {
104+
if (isNaN(value) || !isFinite(value)) {
105+
return value || fallback;
106+
}
107+
if (typeof fixed === 'undefined') {
108+
fixed = 2;
109+
}
110+
if (value && value.toFixed) {
111+
let [tmp, exp] = value.toFixed(fixed).split('e+');
112+
if (exp) {
113+
tmp = parseFloat(tmp).toFixed(fixed)
114+
}
115+
value = parseFloat(tmp);
116+
}
117+
return value;
118+
}
119+
120+
function currency(value, currency, locale) {
121+
if (isNaN(value) || !isFinite(value)) {
122+
return value || fallback;
123+
}
124+
const style = currency ? { style: 'currency', currency } : {};
125+
return new Intl.NumberFormat(locale, style).format(value);
126+
}
127+
128+
/*
129+
function normalize(term, separator) {
130+
term = (term || '').toString();
131+
separator = typeof separator === 'string' ? separator : '_';
132+
let normalized = inflected.parameterize(term, { separator })
133+
.replace(/-/g, separator);
134+
135+
normalized = inflected.humanize(normalized).trim();
136+
normalized = inflected.parameterize(normalized, { separator })
137+
.replace(/-/g, separator);
138+
return normalized;
139+
}
140+
*/
141+
142+
function percent(value, total, format) {
143+
format = format || '';
144+
if (!total || !value) {
145+
return `${0} ${format}`;
146+
}
147+
let percent = (value * 100) / total;
148+
if (format) {
149+
percent = (+percent).toFixed(0);
150+
}
151+
return `${percent} ${format}`;
152+
}
153+
154+
module.exports = {
155+
setLocale,
156+
157+
date,
158+
time,
159+
dateTime,
160+
dateOrTime,
161+
timeSpan,
162+
number,
163+
currency,
164+
percent
165+
};

src/log.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
*
3+
* Copyright 2019 Rightech IoT. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
119
const colors = require('colors/safe');
220
const merge = require('deepmerge');
321
const { inspect } = require('util');
@@ -34,7 +52,7 @@ const TIMEOF = Object.freeze({
3452
function getDefaultOptions() {
3553
return {
3654
level: LEVELS.info,
37-
time : TIMEOF.iso,
55+
time: TIMEOF.iso,
3856
width: {
3957
object: 40
4058
}

src/node.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11

2+
/**
3+
*
4+
* Copyright 2019 Rightech IoT. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
220
const path = require('path');
321
const stackTrace = require('stack-trace');
422

src/safe.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
/*
2-
* name : Rightech IoT Cloud ~.
3-
* description : ~
4-
* author : Oleg Prohazko
1+
/**
2+
*
3+
* Copyright 2019 Rightech IoT. All Rights Reserved.
54
*
6-
* LLC, Komnet, Russian Federation, Moscow, 2016
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
78
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
816
*/
917

1018

@@ -24,7 +32,7 @@ function get(object, path) {
2432
for (let i = 0; i < keys.length; i++) {
2533
const key = keys[i];
2634

27-
if(typeof object === 'number' && isFinite(key)) {
35+
if (typeof object === 'number' && isFinite(key)) {
2836
return bit(object, +key);
2937
}
3038

0 commit comments

Comments
 (0)