@questwork/q-utilities 0.1.7 → 0.1.9

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.
@@ -55,6 +55,7 @@ __webpack_require__.d(__webpack_exports__, {
55
55
  QMeta: () => (/* reexport */ QMeta),
56
56
  Repo: () => (/* reexport */ Repo),
57
57
  Service: () => (/* reexport */ Service),
58
+ Template: () => (/* reexport */ Template),
58
59
  UniqueKeyGenerator: () => (/* reexport */ UniqueKeyGenerator),
59
60
  convertString: () => (/* reexport */ convertString),
60
61
  formatDate: () => (/* reexport */ formatDate),
@@ -960,11 +961,11 @@ class Repo {
960
961
  reject(new Error('more than one is found'))
961
962
  }
962
963
  })
963
- .catch((err) => {
964
- log({ level: 'warn', output: err.toString() })
965
- throw err
966
- })
967
964
  })
965
+ .catch((err) => {
966
+ log({ level: 'warn', output: err.toString() })
967
+ throw err
968
+ })
968
969
  }
969
970
 
970
971
  saveAll({ docs, systemLog }) {
@@ -978,14 +979,14 @@ class Repo {
978
979
  const promise = typeof this.model.saveAll === 'function'
979
980
  ? this.model.saveAll({ docs })
980
981
  : Promise.all(docs.map(async (doc) => {
981
- if (doc) {
982
- const result = await this.saveOne({ doc })
983
- isNew = result.isNew
984
- const _data = result._data || result.data
985
- return _data[0]
986
- }
987
- return null
988
- }))
982
+ if (doc) {
983
+ const result = await this.saveOne({ doc })
984
+ isNew = result.isNew
985
+ const _data = result._data || result.data
986
+ return _data[0]
987
+ }
988
+ return null
989
+ }))
989
990
  return promise.then((savedData) => {
990
991
  if (savedData.length !== 1) isNew = null
991
992
  const result = {
@@ -1145,6 +1146,429 @@ function makeService({ repo }) {
1145
1146
 
1146
1147
 
1147
1148
 
1149
+ ;// ./lib/models/template/templateException.js
1150
+ const TEMPLATE_EXCEPTION_TYPE = {
1151
+ argumentEmptyException: 'Argument is empty',
1152
+ argumentFormatException: 'Incorrect number or format of argument',
1153
+ invalidFuntionException: 'Function Name is invalid',
1154
+ invalidRegExpException: 'Invalid regular expression',
1155
+ isNotAFunctionException: 'Is not a function',
1156
+ notExistException: 'Key does not exist',
1157
+ resultEmptyException: 'Result is empty',
1158
+ resultMoreThanOneException: 'More than one result'
1159
+ }
1160
+
1161
+ class TemplateException extends Error {
1162
+ constructor(message) {
1163
+ super(message)
1164
+ this.message = message
1165
+ }
1166
+ }
1167
+
1168
+
1169
+
1170
+ ;// ./lib/models/template/constants.js
1171
+ const _EMPTY = '_EMPTY'
1172
+ const _FN_NAMES = [
1173
+ 'get', 'map', 'join', 'concatIf', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1174
+ ]
1175
+ const _HIDE = '_HIDE'
1176
+ const _NOT_EMPTY = '_NOT_EMPTY'
1177
+ const _SELF = '_SELF'
1178
+ const TAGS_EJS = ['<%=', '%>']
1179
+ const TAGS_HANDLEBAR = ['{{', '}}']
1180
+
1181
+
1182
+
1183
+ ;// ./lib/models/template/helpers/_eq.js
1184
+
1185
+
1186
+
1187
+
1188
+ function _eq(data, args) {
1189
+ if (args.length !== 3) {
1190
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1191
+ }
1192
+ if (data === null || (typeof data === 'undefined')) {
1193
+ return null
1194
+ }
1195
+ if (args.includes(_SELF)) {
1196
+ args = args.map((arg) => {
1197
+ return (arg === _SELF) ? data : arg
1198
+ })
1199
+ }
1200
+ const expected = args[0]
1201
+ return data === expected ? args[1] : args[2]
1202
+ }
1203
+
1204
+
1205
+
1206
+ ;// ./lib/models/template/helpers/_filterAll.js
1207
+
1208
+
1209
+ const _filterAll_DELIMITER = '~~~'
1210
+
1211
+ function _filterAll(data, args) {
1212
+ try {
1213
+ if (!Array.isArray(args) || args.length === 0) {
1214
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1215
+ }
1216
+ if (!Array.isArray(data) || data.length === 0) {
1217
+ return []
1218
+ }
1219
+ if (typeof data[0] === 'object') {
1220
+ return _existObject(data, args)
1221
+ }
1222
+ if (typeof data[0] === 'string' || typeof data[0] === 'number') {
1223
+ return _exist(data, args)
1224
+ }
1225
+ return []
1226
+ } catch (e) {
1227
+ throw e
1228
+ }
1229
+ }
1230
+
1231
+ function _exist(data, args) {
1232
+ const _str = `${_filterAll_DELIMITER}${args.join(_filterAll_DELIMITER)}${_filterAll_DELIMITER}`
1233
+ return data.filter((e) => {
1234
+ return _str.includes(`${_filterAll_DELIMITER}${e}${_filterAll_DELIMITER}`) && args.includes(e)
1235
+ })
1236
+ }
1237
+
1238
+ function _existObject(data, args) {
1239
+ if (args.length !== 2) {
1240
+ return []
1241
+ }
1242
+ const [key, _args] = args
1243
+ return data.filter((e) => {
1244
+ return _args.includes(e[key])
1245
+ })
1246
+ }
1247
+
1248
+
1249
+
1250
+ ;// ./lib/models/template/helpers/_filterOne.js
1251
+
1252
+
1253
+
1254
+ function _filterOne(data, args) {
1255
+ try {
1256
+ const list = _filterAll(data, args)
1257
+ if (list.length === 1) {
1258
+ return list[0]
1259
+ }
1260
+ if (list.length === 0) {
1261
+ return null
1262
+ }
1263
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.resultMoreThanOneException)
1264
+ } catch (e) {
1265
+ throw e
1266
+ }
1267
+ }
1268
+
1269
+
1270
+
1271
+ ;// ./lib/models/template/helpers/_get.js
1272
+
1273
+
1274
+ function _get(data, key, failover = null) {
1275
+ try {
1276
+ if (key === null || (typeof key === 'undefined') || key === '') {
1277
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1278
+ }
1279
+ if (data === null) {
1280
+ return null
1281
+ }
1282
+ if (key.includes('.')) {
1283
+ const parts = key.split('.')
1284
+ if (parts.length > 1) {
1285
+ const first = parts.shift()
1286
+ const remainingKey = parts.join('.')
1287
+ if (typeof data[first] !== 'undefined') {
1288
+ return _get(data[first], remainingKey, failover)
1289
+ }
1290
+ return _handleFailover(key, failover)
1291
+ }
1292
+ }
1293
+ if (typeof data[key] !== 'undefined') {
1294
+ return data[key]
1295
+ }
1296
+ return _handleFailover(key, failover)
1297
+ } catch (e) {
1298
+ throw e
1299
+ }
1300
+ }
1301
+
1302
+ function _handleFailover(key, failover) {
1303
+ if (failover !== null) {
1304
+ return failover
1305
+ }
1306
+ return null
1307
+ // throw new TemplateException(`Key "${key}" does not exist and no failover`)
1308
+ }
1309
+
1310
+
1311
+
1312
+ ;// ./lib/models/template/helpers/_join.js
1313
+ function _join(data, delimiter) {
1314
+ try {
1315
+ return data.join(delimiter)
1316
+ } catch (e) {
1317
+ throw e
1318
+ }
1319
+ }
1320
+
1321
+
1322
+
1323
+ ;// ./lib/models/template/helpers/_map.js
1324
+
1325
+
1326
+
1327
+ function _map(data, args) {
1328
+ try {
1329
+ if (args.length === 0) {
1330
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1331
+ }
1332
+ if (data === null || (typeof data === 'undefined')) {
1333
+ return null
1334
+ }
1335
+
1336
+ const result = data.reduce((acc, item) => {
1337
+ if (args.length === 1 && Array.isArray(args[0])) {
1338
+ args = args[0]
1339
+ acc.hasFormat = true
1340
+ }
1341
+ const list = args.map((key) => {
1342
+ if (key.includes('.')) {
1343
+ const parts = key.split('.')
1344
+ const first = parts[0]
1345
+ parts.shift()
1346
+ const remainingKey = parts.join('.')
1347
+ return _get(item[first], remainingKey)
1348
+ }
1349
+ if (typeof item[key] !== 'undefined') {
1350
+ return item[key]
1351
+ }
1352
+ return null
1353
+ })
1354
+ if (acc.hasFormat) {
1355
+ acc.content.push(list)
1356
+ } else {
1357
+ acc.content = acc.content.concat(list)
1358
+ }
1359
+ return acc
1360
+ }, {
1361
+ content: [],
1362
+ hasFormat: false
1363
+ })
1364
+ return result.content
1365
+ } catch (e) {
1366
+ throw e
1367
+ }
1368
+ }
1369
+
1370
+
1371
+
1372
+ ;// ./lib/models/template/helpers/index.js
1373
+
1374
+
1375
+
1376
+
1377
+
1378
+
1379
+
1380
+
1381
+
1382
+ ;// ./lib/models/template/template.js
1383
+
1384
+
1385
+
1386
+
1387
+ class Template {
1388
+ constructor({ data }) {
1389
+ this.data = data
1390
+ }
1391
+ static eq(data, args) {
1392
+ return _eq(data, args)
1393
+ }
1394
+ static get(data, key, failover = null) {
1395
+ return _get(data, key, failover)
1396
+ }
1397
+ static join(data, separator = '') {
1398
+ return _join(data, separator)
1399
+ }
1400
+ static map(data, args = []) {
1401
+ return _map(data, args)
1402
+ }
1403
+ static parseFunction(expression) {
1404
+ return _parseFunction(expression, _FN_NAMES)
1405
+ }
1406
+
1407
+ pipe(expression = '') {
1408
+ this.delimiters = expression.substring(0,2) === '<%' ? TAGS_EJS : TAGS_HANDLEBAR
1409
+ const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
1410
+ const match = expression.match(regex)
1411
+ if (match !== null) {
1412
+ try {
1413
+ const functionList = _parseFunction(match[1], _FN_NAMES)
1414
+ return functionList.reduce((acc, fn) => {
1415
+ return _callFunction(acc, fn.name, fn.args)
1416
+ }, this.data)
1417
+ } catch (e) {
1418
+ throw new TemplateException(`Template engine error: ${e.message}`)
1419
+ }
1420
+ }
1421
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalidRegExpException)
1422
+ }
1423
+ }
1424
+
1425
+ function _parseFunction(expression, existFunctionNames) {
1426
+ const regExp = new RegExp(/(\w+)\(([^)]*)\)/)
1427
+ let parts
1428
+ if (expression.includes('|')) {
1429
+ parts = expression.split('|')
1430
+ } else {
1431
+ parts = [expression]
1432
+ }
1433
+ return parts.reduce((acc, part) => {
1434
+ const match = part.match(regExp)
1435
+ if (match !== null) {
1436
+ const functionName = match[1]
1437
+ const parameters = match[2]
1438
+ const paramList = _parseParams(parameters)
1439
+ if (existFunctionNames.includes(functionName)) {
1440
+ acc.push({
1441
+ name: functionName,
1442
+ args: paramList
1443
+ })
1444
+ } else {
1445
+ throw new TemplateException(`${functionName} is not a valid function`)
1446
+ }
1447
+ }
1448
+ return acc
1449
+ }, [])
1450
+ }
1451
+
1452
+ function _parseParams(parameters) {
1453
+ const _parameters = parameters.trim()
1454
+ const regExp = new RegExp(/^[^\w\d\s]+$/)
1455
+ const match = _parameters.match(regExp)
1456
+ if (match !== null) {
1457
+ return [_parameters.substring(1, _parameters.length - 1)]
1458
+ }
1459
+ if (_parameters.includes(',')) {
1460
+ // 用正则表达式匹配逗号,但忽略方括号中的逗号
1461
+ const parts = _splitIgnoringBrackets(_parameters)
1462
+ return parts.map((part) => _parseSinglePart(part))
1463
+ }
1464
+ return [_parseSinglePart(_parameters)]
1465
+ }
1466
+
1467
+ function _splitIgnoringBrackets(input) {
1468
+ const regExp2 = new RegExp(/^\d+(\.\d+)?$/)
1469
+ const regExp = new RegExp(/(?![^\[]*\])\s*,\s*/)
1470
+ const parts = input.split(regExp)
1471
+ return parts.map((part) => {
1472
+ const _part = part.trim()
1473
+ if (_part !== '' && !!_part.match(regExp2)) {
1474
+ // 如果是数字,转换为 num 类型
1475
+ return Number.isNaN(_part) ? _part : parseInt(_part, 10)
1476
+ }
1477
+ // 否则当作字符串处理
1478
+ return _part
1479
+ })
1480
+ }
1481
+
1482
+ function _parseSinglePart(input) {
1483
+ if (typeof input === 'string') {
1484
+ if (input.startsWith('"') && input.endsWith('"')) {
1485
+ // 去掉双引号,返回
1486
+ return input.substring(1, input.length - 1)
1487
+ }
1488
+
1489
+ // 如果是一个列表形式(例如 ["p", "d"] 或 [p, d])
1490
+ if (input.startsWith('[') && input.endsWith(']')) {
1491
+ const listContent = input.substring(1, input.length - 1).trim()
1492
+ if (listContent !== '') {
1493
+ return listContent.split(',').map((item) => {
1494
+ return item.trim().replaceAll('"', '') // 去除可能的双引号
1495
+ })
1496
+ }
1497
+ return []
1498
+ }
1499
+ return input
1500
+ }
1501
+ return input
1502
+ }
1503
+
1504
+ function _callFunction(data, functionName, parameters) {
1505
+ try {
1506
+ let failover
1507
+ switch (functionName) {
1508
+ case 'get':
1509
+ if (parameters.length > 2) {
1510
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1511
+ }
1512
+ if (parameters.length === 2) {
1513
+ failover = parameters[parameters.length - 1]
1514
+ }
1515
+ return _get(data, parameters[0], failover)
1516
+ case 'map':
1517
+ return _map(data, parameters)
1518
+ case 'join':
1519
+ return _join(data, parameters[0])
1520
+ // case 'concatIf':
1521
+ // if (parameters.length != 3) {
1522
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1523
+ // }
1524
+ // var condition = parameters.first;
1525
+ // var success = parameters[1];
1526
+ // var failover = parameters[2];
1527
+ // return _concatIf(data, condition, success, failover);
1528
+ case 'filterOne':
1529
+ return _filterOne(data, parameters)
1530
+ case 'filterAll':
1531
+ return _filterAll(data, parameters)
1532
+ // case 'formatDate':
1533
+ // if (parameters is List<String>) {
1534
+ // return _formatDate(data, parameters);
1535
+ // }
1536
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1537
+ case 'eq':
1538
+ return _eq(data, parameters)
1539
+ // case 'neq':
1540
+ // return _neq(data, parameters);
1541
+ // case 'gt':
1542
+ // return _gt(data, parameters);
1543
+ // case 'gte':
1544
+ // return _gte(data, parameters);
1545
+ // case 'lt':
1546
+ // return _lt(data, parameters);
1547
+ // case 'lte':
1548
+ // return _lte(data, parameters);
1549
+ // case 'isEmpty':
1550
+ // return _isEmpty(data, parameters);
1551
+ // case 'isNotEmpty':
1552
+ // return _isNotEmpty(data, parameters);
1553
+ // case 'toLowerCase':
1554
+ // return _toLowerCase(data);
1555
+ // case 'toUpperCase':
1556
+ // return _toUpperCase(data);
1557
+ default:
1558
+ throw new Error(`${functionName} is not a valid function`)
1559
+ }
1560
+ } catch (e) {
1561
+ throw e
1562
+ }
1563
+ }
1564
+
1565
+
1566
+
1567
+ ;// ./lib/models/template/index.js
1568
+
1569
+
1570
+
1571
+
1148
1572
  ;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
1149
1573
 
1150
1574
 
@@ -1202,6 +1626,7 @@ function _makeSetCode(fieldName, options) {
1202
1626
 
1203
1627
 
1204
1628
 
1629
+
1205
1630
  ;// ./lib/helpers/generalPost/generalPost.js
1206
1631
 
1207
1632
 
package/dist/index.min.js CHANGED
@@ -30,6 +30,7 @@ __webpack_require__.d(__webpack_exports__, {
30
30
  Z8: () => (/* reexport */ QMeta),
31
31
  lc: () => (/* reexport */ Repo),
32
32
  kl: () => (/* reexport */ Service),
33
+ Bj: () => (/* reexport */ Template),
33
34
  _x: () => (/* reexport */ UniqueKeyGenerator),
34
35
  l0: () => (/* reexport */ convertString),
35
36
  Yq: () => (/* reexport */ formatDate),
@@ -935,11 +936,11 @@ class Repo {
935
936
  reject(new Error('more than one is found'))
936
937
  }
937
938
  })
938
- .catch((err) => {
939
- log({ level: 'warn', output: err.toString() })
940
- throw err
941
- })
942
939
  })
940
+ .catch((err) => {
941
+ log({ level: 'warn', output: err.toString() })
942
+ throw err
943
+ })
943
944
  }
944
945
 
945
946
  saveAll({ docs, systemLog }) {
@@ -953,14 +954,14 @@ class Repo {
953
954
  const promise = typeof this.model.saveAll === 'function'
954
955
  ? this.model.saveAll({ docs })
955
956
  : Promise.all(docs.map(async (doc) => {
956
- if (doc) {
957
- const result = await this.saveOne({ doc })
958
- isNew = result.isNew
959
- const _data = result._data || result.data
960
- return _data[0]
961
- }
962
- return null
963
- }))
957
+ if (doc) {
958
+ const result = await this.saveOne({ doc })
959
+ isNew = result.isNew
960
+ const _data = result._data || result.data
961
+ return _data[0]
962
+ }
963
+ return null
964
+ }))
964
965
  return promise.then((savedData) => {
965
966
  if (savedData.length !== 1) isNew = null
966
967
  const result = {
@@ -1120,6 +1121,429 @@ function makeService({ repo }) {
1120
1121
 
1121
1122
 
1122
1123
 
1124
+ ;// ./lib/models/template/templateException.js
1125
+ const TEMPLATE_EXCEPTION_TYPE = {
1126
+ argumentEmptyException: 'Argument is empty',
1127
+ argumentFormatException: 'Incorrect number or format of argument',
1128
+ invalidFuntionException: 'Function Name is invalid',
1129
+ invalidRegExpException: 'Invalid regular expression',
1130
+ isNotAFunctionException: 'Is not a function',
1131
+ notExistException: 'Key does not exist',
1132
+ resultEmptyException: 'Result is empty',
1133
+ resultMoreThanOneException: 'More than one result'
1134
+ }
1135
+
1136
+ class TemplateException extends Error {
1137
+ constructor(message) {
1138
+ super(message)
1139
+ this.message = message
1140
+ }
1141
+ }
1142
+
1143
+
1144
+
1145
+ ;// ./lib/models/template/constants.js
1146
+ const _EMPTY = '_EMPTY'
1147
+ const _FN_NAMES = [
1148
+ 'get', 'map', 'join', 'concatIf', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1149
+ ]
1150
+ const _HIDE = '_HIDE'
1151
+ const _NOT_EMPTY = '_NOT_EMPTY'
1152
+ const _SELF = '_SELF'
1153
+ const TAGS_EJS = ['<%=', '%>']
1154
+ const TAGS_HANDLEBAR = ['{{', '}}']
1155
+
1156
+
1157
+
1158
+ ;// ./lib/models/template/helpers/_eq.js
1159
+
1160
+
1161
+
1162
+
1163
+ function _eq(data, args) {
1164
+ if (args.length !== 3) {
1165
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1166
+ }
1167
+ if (data === null || (typeof data === 'undefined')) {
1168
+ return null
1169
+ }
1170
+ if (args.includes(_SELF)) {
1171
+ args = args.map((arg) => {
1172
+ return (arg === _SELF) ? data : arg
1173
+ })
1174
+ }
1175
+ const expected = args[0]
1176
+ return data === expected ? args[1] : args[2]
1177
+ }
1178
+
1179
+
1180
+
1181
+ ;// ./lib/models/template/helpers/_filterAll.js
1182
+
1183
+
1184
+ const _filterAll_DELIMITER = '~~~'
1185
+
1186
+ function _filterAll(data, args) {
1187
+ try {
1188
+ if (!Array.isArray(args) || args.length === 0) {
1189
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1190
+ }
1191
+ if (!Array.isArray(data) || data.length === 0) {
1192
+ return []
1193
+ }
1194
+ if (typeof data[0] === 'object') {
1195
+ return _existObject(data, args)
1196
+ }
1197
+ if (typeof data[0] === 'string' || typeof data[0] === 'number') {
1198
+ return _exist(data, args)
1199
+ }
1200
+ return []
1201
+ } catch (e) {
1202
+ throw e
1203
+ }
1204
+ }
1205
+
1206
+ function _exist(data, args) {
1207
+ const _str = `${_filterAll_DELIMITER}${args.join(_filterAll_DELIMITER)}${_filterAll_DELIMITER}`
1208
+ return data.filter((e) => {
1209
+ return _str.includes(`${_filterAll_DELIMITER}${e}${_filterAll_DELIMITER}`) && args.includes(e)
1210
+ })
1211
+ }
1212
+
1213
+ function _existObject(data, args) {
1214
+ if (args.length !== 2) {
1215
+ return []
1216
+ }
1217
+ const [key, _args] = args
1218
+ return data.filter((e) => {
1219
+ return _args.includes(e[key])
1220
+ })
1221
+ }
1222
+
1223
+
1224
+
1225
+ ;// ./lib/models/template/helpers/_filterOne.js
1226
+
1227
+
1228
+
1229
+ function _filterOne(data, args) {
1230
+ try {
1231
+ const list = _filterAll(data, args)
1232
+ if (list.length === 1) {
1233
+ return list[0]
1234
+ }
1235
+ if (list.length === 0) {
1236
+ return null
1237
+ }
1238
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.resultMoreThanOneException)
1239
+ } catch (e) {
1240
+ throw e
1241
+ }
1242
+ }
1243
+
1244
+
1245
+
1246
+ ;// ./lib/models/template/helpers/_get.js
1247
+
1248
+
1249
+ function _get(data, key, failover = null) {
1250
+ try {
1251
+ if (key === null || (typeof key === 'undefined') || key === '') {
1252
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1253
+ }
1254
+ if (data === null) {
1255
+ return null
1256
+ }
1257
+ if (key.includes('.')) {
1258
+ const parts = key.split('.')
1259
+ if (parts.length > 1) {
1260
+ const first = parts.shift()
1261
+ const remainingKey = parts.join('.')
1262
+ if (typeof data[first] !== 'undefined') {
1263
+ return _get(data[first], remainingKey, failover)
1264
+ }
1265
+ return _handleFailover(key, failover)
1266
+ }
1267
+ }
1268
+ if (typeof data[key] !== 'undefined') {
1269
+ return data[key]
1270
+ }
1271
+ return _handleFailover(key, failover)
1272
+ } catch (e) {
1273
+ throw e
1274
+ }
1275
+ }
1276
+
1277
+ function _handleFailover(key, failover) {
1278
+ if (failover !== null) {
1279
+ return failover
1280
+ }
1281
+ return null
1282
+ // throw new TemplateException(`Key "${key}" does not exist and no failover`)
1283
+ }
1284
+
1285
+
1286
+
1287
+ ;// ./lib/models/template/helpers/_join.js
1288
+ function _join(data, delimiter) {
1289
+ try {
1290
+ return data.join(delimiter)
1291
+ } catch (e) {
1292
+ throw e
1293
+ }
1294
+ }
1295
+
1296
+
1297
+
1298
+ ;// ./lib/models/template/helpers/_map.js
1299
+
1300
+
1301
+
1302
+ function _map(data, args) {
1303
+ try {
1304
+ if (args.length === 0) {
1305
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1306
+ }
1307
+ if (data === null || (typeof data === 'undefined')) {
1308
+ return null
1309
+ }
1310
+
1311
+ const result = data.reduce((acc, item) => {
1312
+ if (args.length === 1 && Array.isArray(args[0])) {
1313
+ args = args[0]
1314
+ acc.hasFormat = true
1315
+ }
1316
+ const list = args.map((key) => {
1317
+ if (key.includes('.')) {
1318
+ const parts = key.split('.')
1319
+ const first = parts[0]
1320
+ parts.shift()
1321
+ const remainingKey = parts.join('.')
1322
+ return _get(item[first], remainingKey)
1323
+ }
1324
+ if (typeof item[key] !== 'undefined') {
1325
+ return item[key]
1326
+ }
1327
+ return null
1328
+ })
1329
+ if (acc.hasFormat) {
1330
+ acc.content.push(list)
1331
+ } else {
1332
+ acc.content = acc.content.concat(list)
1333
+ }
1334
+ return acc
1335
+ }, {
1336
+ content: [],
1337
+ hasFormat: false
1338
+ })
1339
+ return result.content
1340
+ } catch (e) {
1341
+ throw e
1342
+ }
1343
+ }
1344
+
1345
+
1346
+
1347
+ ;// ./lib/models/template/helpers/index.js
1348
+
1349
+
1350
+
1351
+
1352
+
1353
+
1354
+
1355
+
1356
+
1357
+ ;// ./lib/models/template/template.js
1358
+
1359
+
1360
+
1361
+
1362
+ class Template {
1363
+ constructor({ data }) {
1364
+ this.data = data
1365
+ }
1366
+ static eq(data, args) {
1367
+ return _eq(data, args)
1368
+ }
1369
+ static get(data, key, failover = null) {
1370
+ return _get(data, key, failover)
1371
+ }
1372
+ static join(data, separator = '') {
1373
+ return _join(data, separator)
1374
+ }
1375
+ static map(data, args = []) {
1376
+ return _map(data, args)
1377
+ }
1378
+ static parseFunction(expression) {
1379
+ return _parseFunction(expression, _FN_NAMES)
1380
+ }
1381
+
1382
+ pipe(expression = '') {
1383
+ this.delimiters = expression.substring(0,2) === '<%' ? TAGS_EJS : TAGS_HANDLEBAR
1384
+ const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
1385
+ const match = expression.match(regex)
1386
+ if (match !== null) {
1387
+ try {
1388
+ const functionList = _parseFunction(match[1], _FN_NAMES)
1389
+ return functionList.reduce((acc, fn) => {
1390
+ return _callFunction(acc, fn.name, fn.args)
1391
+ }, this.data)
1392
+ } catch (e) {
1393
+ throw new TemplateException(`Template engine error: ${e.message}`)
1394
+ }
1395
+ }
1396
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalidRegExpException)
1397
+ }
1398
+ }
1399
+
1400
+ function _parseFunction(expression, existFunctionNames) {
1401
+ const regExp = new RegExp(/(\w+)\(([^)]*)\)/)
1402
+ let parts
1403
+ if (expression.includes('|')) {
1404
+ parts = expression.split('|')
1405
+ } else {
1406
+ parts = [expression]
1407
+ }
1408
+ return parts.reduce((acc, part) => {
1409
+ const match = part.match(regExp)
1410
+ if (match !== null) {
1411
+ const functionName = match[1]
1412
+ const parameters = match[2]
1413
+ const paramList = _parseParams(parameters)
1414
+ if (existFunctionNames.includes(functionName)) {
1415
+ acc.push({
1416
+ name: functionName,
1417
+ args: paramList
1418
+ })
1419
+ } else {
1420
+ throw new TemplateException(`${functionName} is not a valid function`)
1421
+ }
1422
+ }
1423
+ return acc
1424
+ }, [])
1425
+ }
1426
+
1427
+ function _parseParams(parameters) {
1428
+ const _parameters = parameters.trim()
1429
+ const regExp = new RegExp(/^[^\w\d\s]+$/)
1430
+ const match = _parameters.match(regExp)
1431
+ if (match !== null) {
1432
+ return [_parameters.substring(1, _parameters.length - 1)]
1433
+ }
1434
+ if (_parameters.includes(',')) {
1435
+ // 用正则表达式匹配逗号,但忽略方括号中的逗号
1436
+ const parts = _splitIgnoringBrackets(_parameters)
1437
+ return parts.map((part) => _parseSinglePart(part))
1438
+ }
1439
+ return [_parseSinglePart(_parameters)]
1440
+ }
1441
+
1442
+ function _splitIgnoringBrackets(input) {
1443
+ const regExp2 = new RegExp(/^\d+(\.\d+)?$/)
1444
+ const regExp = new RegExp(/(?![^\[]*\])\s*,\s*/)
1445
+ const parts = input.split(regExp)
1446
+ return parts.map((part) => {
1447
+ const _part = part.trim()
1448
+ if (_part !== '' && !!_part.match(regExp2)) {
1449
+ // 如果是数字,转换为 num 类型
1450
+ return Number.isNaN(_part) ? _part : parseInt(_part, 10)
1451
+ }
1452
+ // 否则当作字符串处理
1453
+ return _part
1454
+ })
1455
+ }
1456
+
1457
+ function _parseSinglePart(input) {
1458
+ if (typeof input === 'string') {
1459
+ if (input.startsWith('"') && input.endsWith('"')) {
1460
+ // 去掉双引号,返回
1461
+ return input.substring(1, input.length - 1)
1462
+ }
1463
+
1464
+ // 如果是一个列表形式(例如 ["p", "d"] 或 [p, d])
1465
+ if (input.startsWith('[') && input.endsWith(']')) {
1466
+ const listContent = input.substring(1, input.length - 1).trim()
1467
+ if (listContent !== '') {
1468
+ return listContent.split(',').map((item) => {
1469
+ return item.trim().replaceAll('"', '') // 去除可能的双引号
1470
+ })
1471
+ }
1472
+ return []
1473
+ }
1474
+ return input
1475
+ }
1476
+ return input
1477
+ }
1478
+
1479
+ function _callFunction(data, functionName, parameters) {
1480
+ try {
1481
+ let failover
1482
+ switch (functionName) {
1483
+ case 'get':
1484
+ if (parameters.length > 2) {
1485
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1486
+ }
1487
+ if (parameters.length === 2) {
1488
+ failover = parameters[parameters.length - 1]
1489
+ }
1490
+ return _get(data, parameters[0], failover)
1491
+ case 'map':
1492
+ return _map(data, parameters)
1493
+ case 'join':
1494
+ return _join(data, parameters[0])
1495
+ // case 'concatIf':
1496
+ // if (parameters.length != 3) {
1497
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1498
+ // }
1499
+ // var condition = parameters.first;
1500
+ // var success = parameters[1];
1501
+ // var failover = parameters[2];
1502
+ // return _concatIf(data, condition, success, failover);
1503
+ case 'filterOne':
1504
+ return _filterOne(data, parameters)
1505
+ case 'filterAll':
1506
+ return _filterAll(data, parameters)
1507
+ // case 'formatDate':
1508
+ // if (parameters is List<String>) {
1509
+ // return _formatDate(data, parameters);
1510
+ // }
1511
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1512
+ case 'eq':
1513
+ return _eq(data, parameters)
1514
+ // case 'neq':
1515
+ // return _neq(data, parameters);
1516
+ // case 'gt':
1517
+ // return _gt(data, parameters);
1518
+ // case 'gte':
1519
+ // return _gte(data, parameters);
1520
+ // case 'lt':
1521
+ // return _lt(data, parameters);
1522
+ // case 'lte':
1523
+ // return _lte(data, parameters);
1524
+ // case 'isEmpty':
1525
+ // return _isEmpty(data, parameters);
1526
+ // case 'isNotEmpty':
1527
+ // return _isNotEmpty(data, parameters);
1528
+ // case 'toLowerCase':
1529
+ // return _toLowerCase(data);
1530
+ // case 'toUpperCase':
1531
+ // return _toUpperCase(data);
1532
+ default:
1533
+ throw new Error(`${functionName} is not a valid function`)
1534
+ }
1535
+ } catch (e) {
1536
+ throw e
1537
+ }
1538
+ }
1539
+
1540
+
1541
+
1542
+ ;// ./lib/models/template/index.js
1543
+
1544
+
1545
+
1546
+
1123
1547
  ;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
1124
1548
 
1125
1549
 
@@ -1177,6 +1601,7 @@ function _makeSetCode(fieldName, options) {
1177
1601
 
1178
1602
 
1179
1603
 
1604
+
1180
1605
  ;// ./lib/helpers/generalPost/generalPost.js
1181
1606
 
1182
1607
 
@@ -1376,6 +1801,7 @@ var __webpack_exports__Metadata = __webpack_exports__.OS;
1376
1801
  var __webpack_exports__QMeta = __webpack_exports__.Z8;
1377
1802
  var __webpack_exports__Repo = __webpack_exports__.lc;
1378
1803
  var __webpack_exports__Service = __webpack_exports__.kl;
1804
+ var __webpack_exports__Template = __webpack_exports__.Bj;
1379
1805
  var __webpack_exports__UniqueKeyGenerator = __webpack_exports__._x;
1380
1806
  var __webpack_exports__convertString = __webpack_exports__.l0;
1381
1807
  var __webpack_exports__formatDate = __webpack_exports__.Yq;
@@ -1389,4 +1815,4 @@ var __webpack_exports__pReduce = __webpack_exports__.d;
1389
1815
  var __webpack_exports__padZeros = __webpack_exports__.Lv;
1390
1816
  var __webpack_exports__stringFormatter = __webpack_exports__.Qy;
1391
1817
  var __webpack_exports__stringHelper = __webpack_exports__.yO;
1392
- export { __webpack_exports__ApiResponse as ApiResponse, __webpack_exports__KeyValueObject as KeyValueObject, __webpack_exports__Metadata as Metadata, __webpack_exports__QMeta as QMeta, __webpack_exports__Repo as Repo, __webpack_exports__Service as Service, __webpack_exports__UniqueKeyGenerator as UniqueKeyGenerator, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__generalPost as generalPost, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys, __webpack_exports__makeApiResponse as makeApiResponse, __webpack_exports__makeService as makeService, __webpack_exports__objectHelper as objectHelper, __webpack_exports__pReduce as pReduce, __webpack_exports__padZeros as padZeros, __webpack_exports__stringFormatter as stringFormatter, __webpack_exports__stringHelper as stringHelper };
1818
+ export { __webpack_exports__ApiResponse as ApiResponse, __webpack_exports__KeyValueObject as KeyValueObject, __webpack_exports__Metadata as Metadata, __webpack_exports__QMeta as QMeta, __webpack_exports__Repo as Repo, __webpack_exports__Service as Service, __webpack_exports__Template as Template, __webpack_exports__UniqueKeyGenerator as UniqueKeyGenerator, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__generalPost as generalPost, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys, __webpack_exports__makeApiResponse as makeApiResponse, __webpack_exports__makeService as makeService, __webpack_exports__objectHelper as objectHelper, __webpack_exports__pReduce as pReduce, __webpack_exports__padZeros as padZeros, __webpack_exports__stringFormatter as stringFormatter, __webpack_exports__stringHelper as stringHelper };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@questwork/q-utilities",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Questwork QUtilities",
5
5
  "main": "dist/index.min.js",
6
6
  "type": "module",
@@ -10,6 +10,11 @@
10
10
  "default": "./dist/index.min.js"
11
11
  }
12
12
  },
13
+ "scripts": {
14
+ "build": "cross-env NODE_ENV=production minimize=false gulp",
15
+ "lint": "eslint .",
16
+ "test:models": "NODE_ENV=test mocha --exit 'lib/models/test.setup.js' 'lib/models/**/*.spec.js'"
17
+ },
13
18
  "author": {
14
19
  "name": "Questwork Consulting Limited",
15
20
  "email": "info@questwork.com",
@@ -40,10 +45,5 @@
40
45
  },
41
46
  "engines": {
42
47
  "node": ">=10.0.0"
43
- },
44
- "scripts": {
45
- "build": "cross-env NODE_ENV=production minimize=false gulp",
46
- "lint": "eslint .",
47
- "test:models": "NODE_ENV=test mocha --exit 'lib/models/test.setup.js' 'lib/models/**/*.spec.js'"
48
48
  }
49
- }
49
+ }