semanticdb-core 1.0.21 → 1.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { RepresentationType } from '../common/representation';
|
|
2
2
|
import { SchemaType } from './schema';
|
|
3
|
+
export declare type FormatterType = {
|
|
4
|
+
min?: number;
|
|
5
|
+
max?: number;
|
|
6
|
+
formatter: string;
|
|
7
|
+
prefix?: string;
|
|
8
|
+
postfix?: string;
|
|
9
|
+
role?: string;
|
|
10
|
+
};
|
|
3
11
|
export declare type PropertyTypeType = 'ID' | 'timestamp' | 'name' | 'humanName' | 'category' | 'string' | 'tag' | 'image' | 'file' | 'number' | 'int' | 'currency' | 'percentage' | 'duration' | 'order' | 'boolean' | 'date' | 'start_date' | 'end_date' | 'object';
|
|
4
12
|
export declare type PropertyTypePrimalType = 'string' | 'number' | 'boolean' | 'date' | 'object';
|
|
5
13
|
export interface PropertyTypeDraft {
|
|
@@ -41,14 +49,7 @@ export interface PropertyTypeDraft {
|
|
|
41
49
|
ui?: {
|
|
42
50
|
name?: boolean;
|
|
43
51
|
formatter?: string;
|
|
44
|
-
formatters?:
|
|
45
|
-
min?: number;
|
|
46
|
-
max?: number;
|
|
47
|
-
formatter: string;
|
|
48
|
-
prefix?: string;
|
|
49
|
-
postfix?: string;
|
|
50
|
-
role?: string;
|
|
51
|
-
}[];
|
|
52
|
+
formatters?: FormatterType[];
|
|
52
53
|
show_in_detail_only?: boolean;
|
|
53
54
|
hide?: boolean;
|
|
54
55
|
ellipsis?: boolean;
|
|
@@ -1,2 +1,14 @@
|
|
|
1
|
-
import { PropertyType } from './property';
|
|
1
|
+
import { FormatterType, PropertyType } from './property';
|
|
2
|
+
import { SchemaType } from './schema';
|
|
3
|
+
export declare function getNameProperty(schema: SchemaType): PropertyType;
|
|
2
4
|
export declare const getGranularity: (property: PropertyType) => "year" | "month" | "week" | "day" | "hour" | "minute" | "second";
|
|
5
|
+
export declare const getFormatter: (property: PropertyType) => FormatterType | null;
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param schema
|
|
9
|
+
* @param property
|
|
10
|
+
* @param value
|
|
11
|
+
* @param withUnit 是否要在数字最后加上万、亿这种单位
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export declare const formatWithProperty: (schema: SchemaType, property: PropertyType | undefined, value: any, withUnit?: boolean) => string;
|
|
@@ -1,7 +1,111 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getGranularity = void 0;
|
|
6
|
+
exports.formatWithProperty = exports.getFormatter = exports.getGranularity = void 0;
|
|
7
|
+
exports.getNameProperty = getNameProperty;
|
|
8
|
+
const moment_1 = __importDefault(require("moment"));
|
|
9
|
+
const numeral_1 = __importDefault(require("numeral"));
|
|
10
|
+
function getNameProperty(schema) {
|
|
11
|
+
let nameProp = schema.properties.find((p) => { var _a; return p.is_name || ((_a = p.ui) === null || _a === void 0 ? void 0 : _a.name); });
|
|
12
|
+
if (!nameProp) {
|
|
13
|
+
nameProp = schema.properties.find((p) => p.type === 'ID');
|
|
14
|
+
}
|
|
15
|
+
if (!nameProp) {
|
|
16
|
+
nameProp = { name: '_id', constraints: { unique: true } };
|
|
17
|
+
}
|
|
18
|
+
return nameProp;
|
|
19
|
+
}
|
|
4
20
|
const getGranularity = (property) => {
|
|
5
21
|
return property.granularity || 'day';
|
|
6
22
|
};
|
|
7
23
|
exports.getGranularity = getGranularity;
|
|
24
|
+
const getFormatter = (property) => {
|
|
25
|
+
var _a, _b, _c, _d;
|
|
26
|
+
// 单个的formatter优先。因为table模式下要决定单个formatter
|
|
27
|
+
if ((_a = property.ui) === null || _a === void 0 ? void 0 : _a.formatter) {
|
|
28
|
+
return {
|
|
29
|
+
formatter: (_b = property.ui) === null || _b === void 0 ? void 0 : _b.formatter,
|
|
30
|
+
prefix: '',
|
|
31
|
+
postfix: '',
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if ((_c = property.ui) === null || _c === void 0 ? void 0 : _c.formatters) {
|
|
35
|
+
// 拿第一个
|
|
36
|
+
const [formatter] = (_d = property.ui) === null || _d === void 0 ? void 0 : _d.formatters;
|
|
37
|
+
if (formatter) {
|
|
38
|
+
if (!formatter.prefix)
|
|
39
|
+
formatter.prefix = '';
|
|
40
|
+
if (!formatter.postfix)
|
|
41
|
+
formatter.postfix = '';
|
|
42
|
+
return formatter;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
exports.getFormatter = getFormatter;
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* @param schema
|
|
51
|
+
* @param property
|
|
52
|
+
* @param value
|
|
53
|
+
* @param withUnit 是否要在数字最后加上万、亿这种单位
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
const formatWithProperty = (schema, property, value, withUnit = false) => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
if (!property)
|
|
59
|
+
return `${value}`;
|
|
60
|
+
if (value === null)
|
|
61
|
+
return '-';
|
|
62
|
+
const formatter = (0, exports.getFormatter)(property);
|
|
63
|
+
let finalValue = `${value}`;
|
|
64
|
+
if (property.type === 'object' && typeof value === 'object') {
|
|
65
|
+
const nameProperty = getNameProperty(schema);
|
|
66
|
+
finalValue = value[nameProperty.name];
|
|
67
|
+
}
|
|
68
|
+
else if (property.primal_type === 'date') {
|
|
69
|
+
if (formatter) {
|
|
70
|
+
finalValue = (0, moment_1.default)(value).format(formatter.formatter);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else if (typeof value === 'string') {
|
|
74
|
+
finalValue = value;
|
|
75
|
+
}
|
|
76
|
+
else if (formatter) {
|
|
77
|
+
finalValue = (0, numeral_1.default)(value).format(formatter.formatter);
|
|
78
|
+
}
|
|
79
|
+
else if (property.type === 'percentage') {
|
|
80
|
+
finalValue = (0, numeral_1.default)(value).format('0.00%');
|
|
81
|
+
}
|
|
82
|
+
else if (property.type === 'int') {
|
|
83
|
+
finalValue = (0, numeral_1.default)(value).format('0,0');
|
|
84
|
+
}
|
|
85
|
+
else if (property.type === 'currency') {
|
|
86
|
+
finalValue = (0, numeral_1.default)(value).format('0,0.00');
|
|
87
|
+
}
|
|
88
|
+
else if (property.primal_type === 'number') {
|
|
89
|
+
finalValue = (0, numeral_1.default)(value).format('0,0.0');
|
|
90
|
+
}
|
|
91
|
+
if (withUnit && property.type !== 'percentage') {
|
|
92
|
+
let unit = '';
|
|
93
|
+
if (((_a = formatter === null || formatter === void 0 ? void 0 : formatter.formatter) === null || _a === void 0 ? void 0 : _a.endsWith('w')) &&
|
|
94
|
+
(!(property === null || property === void 0 ? void 0 : property.unit) || !(property === null || property === void 0 ? void 0 : property.unit.startsWith('万')))) {
|
|
95
|
+
unit += '万';
|
|
96
|
+
}
|
|
97
|
+
else if (((_b = formatter === null || formatter === void 0 ? void 0 : formatter.formatter) === null || _b === void 0 ? void 0 : _b.endsWith('y')) &&
|
|
98
|
+
(!(property === null || property === void 0 ? void 0 : property.unit) || !(property === null || property === void 0 ? void 0 : property.unit.startsWith('亿')))) {
|
|
99
|
+
unit += '亿';
|
|
100
|
+
}
|
|
101
|
+
if (property === null || property === void 0 ? void 0 : property.unit) {
|
|
102
|
+
unit += property.unit;
|
|
103
|
+
}
|
|
104
|
+
if (formatter) {
|
|
105
|
+
unit = `${formatter.prefix}${unit}${formatter.postfix}`;
|
|
106
|
+
}
|
|
107
|
+
return finalValue + unit;
|
|
108
|
+
}
|
|
109
|
+
return finalValue;
|
|
110
|
+
};
|
|
111
|
+
exports.formatWithProperty = formatWithProperty;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "semanticdb-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.22",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"@types/jest": "^29.5.14",
|
|
27
27
|
"@types/lodash": "^4.17.16",
|
|
28
28
|
"@types/node": "^22.13.10",
|
|
29
|
+
"@types/numeral": "^2.0.5",
|
|
29
30
|
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
30
31
|
"@typescript-eslint/parser": "^8.26.1",
|
|
31
32
|
"eslint": "^9.22.0",
|
|
@@ -38,6 +39,7 @@
|
|
|
38
39
|
"typescript": "^5.8.2"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"moment": "^2.30.1"
|
|
42
|
+
"moment": "^2.30.1",
|
|
43
|
+
"numeral": "^2.0.6"
|
|
42
44
|
}
|
|
43
45
|
}
|