@questwork/q-utilities 0.1.6 → 0.1.8

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),
@@ -1091,7 +1092,13 @@ class Service {
1091
1092
  return this.repo.init(options)
1092
1093
  }
1093
1094
  initFromArray(arr = []) {
1094
- return arr.map((i) => this.init(i))
1095
+ if (Array.isArray(arr)) {
1096
+ return arr.map((a) => this.init(a))
1097
+ }
1098
+ return []
1099
+ }
1100
+ initOnlyValidFromArray(arr = []) {
1101
+ return this.initFromArray(arr).filter((i) => i)
1095
1102
  }
1096
1103
 
1097
1104
  async saveAll({ docs = [], systemLog } = {}) {
@@ -1139,6 +1146,393 @@ function makeService({ repo }) {
1139
1146
 
1140
1147
 
1141
1148
 
1149
+ ;// ./lib/models/template/constants.js
1150
+ const _EMPTY = '_EMPTY'
1151
+ const _FN_NAMES = [
1152
+ 'get', 'map', 'join', 'concatIf', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1153
+ ]
1154
+ const _HIDE = '_HIDE'
1155
+ const _NOT_EMPTY = '_NOT_EMPTY'
1156
+ const _SELF = '_SELF'
1157
+ const TAGS_EJS = ['<%=', '%>']
1158
+ const TAGS_HANDLEBAR = ['{{', '}}']
1159
+
1160
+
1161
+
1162
+ ;// ./lib/models/template/helpers/_eq.js
1163
+
1164
+
1165
+ function _eq(data, args) {
1166
+ if (data === null || (typeof data === 'undefined') || args.length !== 3) {
1167
+ return null
1168
+ }
1169
+ if (args.includes(_SELF)) {
1170
+ args = args.map((arg) => {
1171
+ return (arg === _SELF) ? data : arg
1172
+ })
1173
+ }
1174
+ const expected = args[0]
1175
+ return data === expected ? args[1] : args[2]
1176
+ }
1177
+
1178
+
1179
+
1180
+ ;// ./lib/models/template/templateException.js
1181
+ const TEMPLATE_EXCEPTION_TYPE = {
1182
+ argumentEmptyException: 'Argument is empty',
1183
+ argumentFormatException: 'Incorrect number or format of argument',
1184
+ invalidFuntionException: 'Function Name is invalid',
1185
+ invalidRegExpException: 'Invalid regular expression',
1186
+ isNotAFunctionException: 'Is not a function',
1187
+ notExistException: 'Key does not exist',
1188
+ resultEmptyException: 'Result is empty',
1189
+ resultMoreThanOneException: 'More than one result'
1190
+ }
1191
+
1192
+ class TemplateException extends Error {
1193
+ constructor(message) {
1194
+ super(message)
1195
+ this.message = message
1196
+ }
1197
+ }
1198
+
1199
+
1200
+
1201
+ ;// ./lib/models/template/helpers/_filterAll.js
1202
+
1203
+
1204
+ const _filterAll_DELIMITER = '~~~'
1205
+
1206
+ function _filterAll(data, args) {
1207
+ if (!Array.isArray(data) || data.length === 0 || !Array.isArray(args) || args.length === 0) {
1208
+ return []
1209
+ }
1210
+
1211
+ if (typeof data[0] === 'object') {
1212
+ return _existObject(data, args)
1213
+ }
1214
+
1215
+ if (typeof data[0] === 'string' || typeof data[0] === 'number') {
1216
+ return _exist(data, args)
1217
+ }
1218
+
1219
+ return []
1220
+ }
1221
+
1222
+ function _exist(data, args) {
1223
+ const _str = `${_filterAll_DELIMITER}${args.join(_filterAll_DELIMITER)}${_filterAll_DELIMITER}`
1224
+ return data.filter((e) => {
1225
+ return _str.includes(`${_filterAll_DELIMITER}${e}${_filterAll_DELIMITER}`) && args.includes(e)
1226
+ })
1227
+ }
1228
+
1229
+ function _existObject(data, args) {
1230
+ if (args.length !== 2) {
1231
+ return []
1232
+ }
1233
+ const [key, _args] = args
1234
+ return data.filter((e) => {
1235
+ return _args.includes(e[key])
1236
+ })
1237
+ }
1238
+
1239
+
1240
+
1241
+ ;// ./lib/models/template/helpers/_filterOne.js
1242
+
1243
+
1244
+
1245
+ function _filterOne(data, args) {
1246
+ const list = _filterAll(data, args)
1247
+ if (list.length > 0) {
1248
+ return list[0]
1249
+ }
1250
+ return null
1251
+ }
1252
+
1253
+
1254
+
1255
+ ;// ./lib/models/template/helpers/_get.js
1256
+
1257
+
1258
+ function _get(data, key, failover = null) {
1259
+ if (data === null || (typeof data === 'undefined') || key === '') {
1260
+ return null
1261
+ // throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1262
+ }
1263
+
1264
+ if (key.includes('.')) {
1265
+ const parts = key.split('.')
1266
+ if (parts.length > 1) {
1267
+ const first = parts.shift()
1268
+ const remainingKey = parts.join('.')
1269
+ if (typeof data[first] !== 'undefined') {
1270
+ return _get(data[first], remainingKey, failover)
1271
+ }
1272
+ return _handleFailover(key, failover)
1273
+ }
1274
+ }
1275
+
1276
+ if (typeof data[key] !== 'undefined') {
1277
+ return data[key]
1278
+ }
1279
+
1280
+ return _handleFailover(key, failover)
1281
+ }
1282
+
1283
+ function _handleFailover(key, failover) {
1284
+ if (failover !== null) {
1285
+ return failover
1286
+ }
1287
+ return null
1288
+ // throw new TemplateException(`Key "${key}" does not exist and no failover`)
1289
+ }
1290
+
1291
+
1292
+
1293
+ ;// ./lib/models/template/helpers/_join.js
1294
+ function _join(data, delimiter) {
1295
+ return data.join(delimiter)
1296
+ }
1297
+
1298
+
1299
+
1300
+ ;// ./lib/models/template/helpers/_map.js
1301
+
1302
+
1303
+
1304
+ function _map(data, args) {
1305
+ if (data === null || (typeof data === 'undefined') || args.length === 0) {
1306
+ return null
1307
+ // throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1308
+ }
1309
+
1310
+ const result = data.reduce((acc, item) => {
1311
+ if (args.length === 1 && Array.isArray(args[0])) {
1312
+ args = args[0]
1313
+ acc.hasFormat = true
1314
+ }
1315
+ const list = args.map((key) => {
1316
+ if (key.includes('.')) {
1317
+ const parts = key.split('.')
1318
+ const first = parts[0]
1319
+ parts.shift()
1320
+ const remainingKey = parts.join('.')
1321
+ return _get(item[first], remainingKey)
1322
+ }
1323
+ if (typeof item[key] !== 'undefined') {
1324
+ return item[key]
1325
+ }
1326
+ return null
1327
+ // throw new TemplateException('Key "$key" does not exist')
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
+ }
1341
+
1342
+
1343
+
1344
+ ;// ./lib/models/template/helpers/index.js
1345
+
1346
+
1347
+
1348
+
1349
+
1350
+
1351
+
1352
+
1353
+
1354
+ ;// ./lib/models/template/template.js
1355
+
1356
+
1357
+
1358
+
1359
+ class Template {
1360
+ constructor({ data }) {
1361
+ this.data = data
1362
+ }
1363
+ static eq(data, args) {
1364
+ return _eq(data, args)
1365
+ }
1366
+ static get(data, key, failover = null) {
1367
+ return _get(data, key, failover)
1368
+ }
1369
+ static join(data, separator = '') {
1370
+ return _join(data, separator)
1371
+ }
1372
+ static map(data, args = []) {
1373
+ return _map(data, args)
1374
+ }
1375
+ static parseFunction(expression) {
1376
+ return _parseFunction(expression, _FN_NAMES)
1377
+ }
1378
+
1379
+ pipe(expression = '') {
1380
+ this.delimiters = expression.substring(0,2) === '<%' ? TAGS_EJS : TAGS_HANDLEBAR
1381
+ const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
1382
+ const match = expression.match(regex)
1383
+ if (match !== null) {
1384
+ const functionList = _parseFunction(match[1], _FN_NAMES)
1385
+ return functionList.reduce((acc, fn) => {
1386
+ return _callFunction(acc, fn.name, fn.args)
1387
+ }, this.data)
1388
+ }
1389
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalu)
1390
+ }
1391
+ }
1392
+
1393
+ function _parseFunction(expression, existFunctionNames) {
1394
+ const regExp = new RegExp(/(\w+)\(([^)]*)\)/)
1395
+ let parts
1396
+ if (expression.includes('|')) {
1397
+ parts = expression.split('|')
1398
+ } else {
1399
+ parts = [expression]
1400
+ }
1401
+ return parts.reduce((acc, part) => {
1402
+ const match = part.match(regExp)
1403
+ if (match !== null) {
1404
+ const functionName = match[1]
1405
+ const parameters = match[2]
1406
+ const paramList = _parseParams(parameters)
1407
+ if (existFunctionNames.includes(functionName)) {
1408
+ acc.push({
1409
+ name: functionName,
1410
+ args: paramList
1411
+ })
1412
+ } else {
1413
+ throw new TemplateException(`${functionName} is not a valid function`)
1414
+ }
1415
+ }
1416
+ return acc
1417
+ }, [])
1418
+ }
1419
+
1420
+ function _parseParams(parameters) {
1421
+ const _parameters = parameters.trim()
1422
+ const regExp = new RegExp(/^[^\w\d\s]+$/)
1423
+ const match = _parameters.match(regExp)
1424
+ if (match !== null) {
1425
+ return [_parameters.substring(1, _parameters.length - 1)]
1426
+ }
1427
+ if (_parameters.includes(',')) {
1428
+ // 用正则表达式匹配逗号,但忽略方括号中的逗号
1429
+ const parts = _splitIgnoringBrackets(_parameters)
1430
+ return parts.map((part) => _parseSinglePart(part))
1431
+ }
1432
+ return [_parseSinglePart(_parameters)]
1433
+ }
1434
+
1435
+ function _splitIgnoringBrackets(input) {
1436
+ const regExp2 = new RegExp(/^\d+(\.\d+)?$/)
1437
+ const regExp = new RegExp(/(?![^\[]*\])\s*,\s*/)
1438
+ const parts = input.split(regExp)
1439
+ return parts.map((part) => {
1440
+ const _part = part.trim()
1441
+ if (_part !== '' && !!_part.match(regExp2)) {
1442
+ // 如果是数字,转换为 num 类型
1443
+ return Number.isNaN(_part) ? _part : parseInt(_part, 10)
1444
+ }
1445
+ // 否则当作字符串处理
1446
+ return _part
1447
+ })
1448
+ }
1449
+
1450
+ function _parseSinglePart(input) {
1451
+ if (typeof input === 'string') {
1452
+ if (input.startsWith('"') && input.endsWith('"')) {
1453
+ // 去掉双引号,返回
1454
+ return input.substring(1, input.length - 1)
1455
+ }
1456
+
1457
+ // 如果是一个列表形式(例如 ["p", "d"] 或 [p, d])
1458
+ if (input.startsWith('[') && input.endsWith(']')) {
1459
+ const listContent = input.substring(1, input.length - 1).trim()
1460
+ if (listContent !== '') {
1461
+ return listContent.split(',').map((item) => {
1462
+ return item.trim().replaceAll('"', '') // 去除可能的双引号
1463
+ })
1464
+ }
1465
+ return []
1466
+ }
1467
+ return input
1468
+ }
1469
+ return input
1470
+ }
1471
+
1472
+ function _callFunction(data, functionName, parameters) {
1473
+ let failover
1474
+ switch (functionName) {
1475
+ case 'get':
1476
+ if (parameters.length > 2) {
1477
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1478
+ }
1479
+ if (parameters.length === 2) {
1480
+ failover = parameters[parameters.length - 1]
1481
+ }
1482
+ return _get(data, parameters[0], failover)
1483
+ case 'map':
1484
+ return _map(data, parameters)
1485
+ case 'join':
1486
+ return _join(data, parameters[0])
1487
+ // case 'concatIf':
1488
+ // if (parameters.length != 3) {
1489
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1490
+ // }
1491
+ // var condition = parameters.first;
1492
+ // var success = parameters[1];
1493
+ // var failover = parameters[2];
1494
+ // return _concatIf(data, condition, success, failover);
1495
+ case 'filterOne':
1496
+ return _filterOne(data, parameters)
1497
+ case 'filterAll':
1498
+ return _filterAll(data, parameters)
1499
+ // case 'formatDate':
1500
+ // if (parameters is List<String>) {
1501
+ // return _formatDate(data, parameters);
1502
+ // }
1503
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1504
+ case 'eq':
1505
+ return _eq(data, parameters)
1506
+ // case 'neq':
1507
+ // return _neq(data, parameters);
1508
+ // case 'gt':
1509
+ // return _gt(data, parameters);
1510
+ // case 'gte':
1511
+ // return _gte(data, parameters);
1512
+ // case 'lt':
1513
+ // return _lt(data, parameters);
1514
+ // case 'lte':
1515
+ // return _lte(data, parameters);
1516
+ // case 'isEmpty':
1517
+ // return _isEmpty(data, parameters);
1518
+ // case 'isNotEmpty':
1519
+ // return _isNotEmpty(data, parameters);
1520
+ // case 'toLowerCase':
1521
+ // return _toLowerCase(data);
1522
+ // case 'toUpperCase':
1523
+ // return _toUpperCase(data);
1524
+ default:
1525
+ throw new Error(`${functionName} is not a valid function`)
1526
+ }
1527
+ }
1528
+
1529
+
1530
+
1531
+ ;// ./lib/models/template/index.js
1532
+
1533
+
1534
+
1535
+
1142
1536
  ;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
1143
1537
 
1144
1538
 
@@ -1196,6 +1590,7 @@ function _makeSetCode(fieldName, options) {
1196
1590
 
1197
1591
 
1198
1592
 
1593
+
1199
1594
  ;// ./lib/helpers/generalPost/generalPost.js
1200
1595
 
1201
1596
 
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),
@@ -1066,7 +1067,13 @@ class Service {
1066
1067
  return this.repo.init(options)
1067
1068
  }
1068
1069
  initFromArray(arr = []) {
1069
- return arr.map((i) => this.init(i))
1070
+ if (Array.isArray(arr)) {
1071
+ return arr.map((a) => this.init(a))
1072
+ }
1073
+ return []
1074
+ }
1075
+ initOnlyValidFromArray(arr = []) {
1076
+ return this.initFromArray(arr).filter((i) => i)
1070
1077
  }
1071
1078
 
1072
1079
  async saveAll({ docs = [], systemLog } = {}) {
@@ -1114,6 +1121,393 @@ function makeService({ repo }) {
1114
1121
 
1115
1122
 
1116
1123
 
1124
+ ;// ./lib/models/template/constants.js
1125
+ const _EMPTY = '_EMPTY'
1126
+ const _FN_NAMES = [
1127
+ 'get', 'map', 'join', 'concatIf', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1128
+ ]
1129
+ const _HIDE = '_HIDE'
1130
+ const _NOT_EMPTY = '_NOT_EMPTY'
1131
+ const _SELF = '_SELF'
1132
+ const TAGS_EJS = ['<%=', '%>']
1133
+ const TAGS_HANDLEBAR = ['{{', '}}']
1134
+
1135
+
1136
+
1137
+ ;// ./lib/models/template/helpers/_eq.js
1138
+
1139
+
1140
+ function _eq(data, args) {
1141
+ if (data === null || (typeof data === 'undefined') || args.length !== 3) {
1142
+ return null
1143
+ }
1144
+ if (args.includes(_SELF)) {
1145
+ args = args.map((arg) => {
1146
+ return (arg === _SELF) ? data : arg
1147
+ })
1148
+ }
1149
+ const expected = args[0]
1150
+ return data === expected ? args[1] : args[2]
1151
+ }
1152
+
1153
+
1154
+
1155
+ ;// ./lib/models/template/templateException.js
1156
+ const TEMPLATE_EXCEPTION_TYPE = {
1157
+ argumentEmptyException: 'Argument is empty',
1158
+ argumentFormatException: 'Incorrect number or format of argument',
1159
+ invalidFuntionException: 'Function Name is invalid',
1160
+ invalidRegExpException: 'Invalid regular expression',
1161
+ isNotAFunctionException: 'Is not a function',
1162
+ notExistException: 'Key does not exist',
1163
+ resultEmptyException: 'Result is empty',
1164
+ resultMoreThanOneException: 'More than one result'
1165
+ }
1166
+
1167
+ class TemplateException extends Error {
1168
+ constructor(message) {
1169
+ super(message)
1170
+ this.message = message
1171
+ }
1172
+ }
1173
+
1174
+
1175
+
1176
+ ;// ./lib/models/template/helpers/_filterAll.js
1177
+
1178
+
1179
+ const _filterAll_DELIMITER = '~~~'
1180
+
1181
+ function _filterAll(data, args) {
1182
+ if (!Array.isArray(data) || data.length === 0 || !Array.isArray(args) || args.length === 0) {
1183
+ return []
1184
+ }
1185
+
1186
+ if (typeof data[0] === 'object') {
1187
+ return _existObject(data, args)
1188
+ }
1189
+
1190
+ if (typeof data[0] === 'string' || typeof data[0] === 'number') {
1191
+ return _exist(data, args)
1192
+ }
1193
+
1194
+ return []
1195
+ }
1196
+
1197
+ function _exist(data, args) {
1198
+ const _str = `${_filterAll_DELIMITER}${args.join(_filterAll_DELIMITER)}${_filterAll_DELIMITER}`
1199
+ return data.filter((e) => {
1200
+ return _str.includes(`${_filterAll_DELIMITER}${e}${_filterAll_DELIMITER}`) && args.includes(e)
1201
+ })
1202
+ }
1203
+
1204
+ function _existObject(data, args) {
1205
+ if (args.length !== 2) {
1206
+ return []
1207
+ }
1208
+ const [key, _args] = args
1209
+ return data.filter((e) => {
1210
+ return _args.includes(e[key])
1211
+ })
1212
+ }
1213
+
1214
+
1215
+
1216
+ ;// ./lib/models/template/helpers/_filterOne.js
1217
+
1218
+
1219
+
1220
+ function _filterOne(data, args) {
1221
+ const list = _filterAll(data, args)
1222
+ if (list.length > 0) {
1223
+ return list[0]
1224
+ }
1225
+ return null
1226
+ }
1227
+
1228
+
1229
+
1230
+ ;// ./lib/models/template/helpers/_get.js
1231
+
1232
+
1233
+ function _get(data, key, failover = null) {
1234
+ if (data === null || (typeof data === 'undefined') || key === '') {
1235
+ return null
1236
+ // throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1237
+ }
1238
+
1239
+ if (key.includes('.')) {
1240
+ const parts = key.split('.')
1241
+ if (parts.length > 1) {
1242
+ const first = parts.shift()
1243
+ const remainingKey = parts.join('.')
1244
+ if (typeof data[first] !== 'undefined') {
1245
+ return _get(data[first], remainingKey, failover)
1246
+ }
1247
+ return _handleFailover(key, failover)
1248
+ }
1249
+ }
1250
+
1251
+ if (typeof data[key] !== 'undefined') {
1252
+ return data[key]
1253
+ }
1254
+
1255
+ return _handleFailover(key, failover)
1256
+ }
1257
+
1258
+ function _handleFailover(key, failover) {
1259
+ if (failover !== null) {
1260
+ return failover
1261
+ }
1262
+ return null
1263
+ // throw new TemplateException(`Key "${key}" does not exist and no failover`)
1264
+ }
1265
+
1266
+
1267
+
1268
+ ;// ./lib/models/template/helpers/_join.js
1269
+ function _join(data, delimiter) {
1270
+ return data.join(delimiter)
1271
+ }
1272
+
1273
+
1274
+
1275
+ ;// ./lib/models/template/helpers/_map.js
1276
+
1277
+
1278
+
1279
+ function _map(data, args) {
1280
+ if (data === null || (typeof data === 'undefined') || args.length === 0) {
1281
+ return null
1282
+ // throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1283
+ }
1284
+
1285
+ const result = data.reduce((acc, item) => {
1286
+ if (args.length === 1 && Array.isArray(args[0])) {
1287
+ args = args[0]
1288
+ acc.hasFormat = true
1289
+ }
1290
+ const list = args.map((key) => {
1291
+ if (key.includes('.')) {
1292
+ const parts = key.split('.')
1293
+ const first = parts[0]
1294
+ parts.shift()
1295
+ const remainingKey = parts.join('.')
1296
+ return _get(item[first], remainingKey)
1297
+ }
1298
+ if (typeof item[key] !== 'undefined') {
1299
+ return item[key]
1300
+ }
1301
+ return null
1302
+ // throw new TemplateException('Key "$key" does not exist')
1303
+ })
1304
+ if (acc.hasFormat) {
1305
+ acc.content.push(list)
1306
+ } else {
1307
+ acc.content = acc.content.concat(list)
1308
+ }
1309
+ return acc
1310
+ }, {
1311
+ content: [],
1312
+ hasFormat: false
1313
+ })
1314
+ return result.content
1315
+ }
1316
+
1317
+
1318
+
1319
+ ;// ./lib/models/template/helpers/index.js
1320
+
1321
+
1322
+
1323
+
1324
+
1325
+
1326
+
1327
+
1328
+
1329
+ ;// ./lib/models/template/template.js
1330
+
1331
+
1332
+
1333
+
1334
+ class Template {
1335
+ constructor({ data }) {
1336
+ this.data = data
1337
+ }
1338
+ static eq(data, args) {
1339
+ return _eq(data, args)
1340
+ }
1341
+ static get(data, key, failover = null) {
1342
+ return _get(data, key, failover)
1343
+ }
1344
+ static join(data, separator = '') {
1345
+ return _join(data, separator)
1346
+ }
1347
+ static map(data, args = []) {
1348
+ return _map(data, args)
1349
+ }
1350
+ static parseFunction(expression) {
1351
+ return _parseFunction(expression, _FN_NAMES)
1352
+ }
1353
+
1354
+ pipe(expression = '') {
1355
+ this.delimiters = expression.substring(0,2) === '<%' ? TAGS_EJS : TAGS_HANDLEBAR
1356
+ const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
1357
+ const match = expression.match(regex)
1358
+ if (match !== null) {
1359
+ const functionList = _parseFunction(match[1], _FN_NAMES)
1360
+ return functionList.reduce((acc, fn) => {
1361
+ return _callFunction(acc, fn.name, fn.args)
1362
+ }, this.data)
1363
+ }
1364
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalu)
1365
+ }
1366
+ }
1367
+
1368
+ function _parseFunction(expression, existFunctionNames) {
1369
+ const regExp = new RegExp(/(\w+)\(([^)]*)\)/)
1370
+ let parts
1371
+ if (expression.includes('|')) {
1372
+ parts = expression.split('|')
1373
+ } else {
1374
+ parts = [expression]
1375
+ }
1376
+ return parts.reduce((acc, part) => {
1377
+ const match = part.match(regExp)
1378
+ if (match !== null) {
1379
+ const functionName = match[1]
1380
+ const parameters = match[2]
1381
+ const paramList = _parseParams(parameters)
1382
+ if (existFunctionNames.includes(functionName)) {
1383
+ acc.push({
1384
+ name: functionName,
1385
+ args: paramList
1386
+ })
1387
+ } else {
1388
+ throw new TemplateException(`${functionName} is not a valid function`)
1389
+ }
1390
+ }
1391
+ return acc
1392
+ }, [])
1393
+ }
1394
+
1395
+ function _parseParams(parameters) {
1396
+ const _parameters = parameters.trim()
1397
+ const regExp = new RegExp(/^[^\w\d\s]+$/)
1398
+ const match = _parameters.match(regExp)
1399
+ if (match !== null) {
1400
+ return [_parameters.substring(1, _parameters.length - 1)]
1401
+ }
1402
+ if (_parameters.includes(',')) {
1403
+ // 用正则表达式匹配逗号,但忽略方括号中的逗号
1404
+ const parts = _splitIgnoringBrackets(_parameters)
1405
+ return parts.map((part) => _parseSinglePart(part))
1406
+ }
1407
+ return [_parseSinglePart(_parameters)]
1408
+ }
1409
+
1410
+ function _splitIgnoringBrackets(input) {
1411
+ const regExp2 = new RegExp(/^\d+(\.\d+)?$/)
1412
+ const regExp = new RegExp(/(?![^\[]*\])\s*,\s*/)
1413
+ const parts = input.split(regExp)
1414
+ return parts.map((part) => {
1415
+ const _part = part.trim()
1416
+ if (_part !== '' && !!_part.match(regExp2)) {
1417
+ // 如果是数字,转换为 num 类型
1418
+ return Number.isNaN(_part) ? _part : parseInt(_part, 10)
1419
+ }
1420
+ // 否则当作字符串处理
1421
+ return _part
1422
+ })
1423
+ }
1424
+
1425
+ function _parseSinglePart(input) {
1426
+ if (typeof input === 'string') {
1427
+ if (input.startsWith('"') && input.endsWith('"')) {
1428
+ // 去掉双引号,返回
1429
+ return input.substring(1, input.length - 1)
1430
+ }
1431
+
1432
+ // 如果是一个列表形式(例如 ["p", "d"] 或 [p, d])
1433
+ if (input.startsWith('[') && input.endsWith(']')) {
1434
+ const listContent = input.substring(1, input.length - 1).trim()
1435
+ if (listContent !== '') {
1436
+ return listContent.split(',').map((item) => {
1437
+ return item.trim().replaceAll('"', '') // 去除可能的双引号
1438
+ })
1439
+ }
1440
+ return []
1441
+ }
1442
+ return input
1443
+ }
1444
+ return input
1445
+ }
1446
+
1447
+ function _callFunction(data, functionName, parameters) {
1448
+ let failover
1449
+ switch (functionName) {
1450
+ case 'get':
1451
+ if (parameters.length > 2) {
1452
+ throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1453
+ }
1454
+ if (parameters.length === 2) {
1455
+ failover = parameters[parameters.length - 1]
1456
+ }
1457
+ return _get(data, parameters[0], failover)
1458
+ case 'map':
1459
+ return _map(data, parameters)
1460
+ case 'join':
1461
+ return _join(data, parameters[0])
1462
+ // case 'concatIf':
1463
+ // if (parameters.length != 3) {
1464
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1465
+ // }
1466
+ // var condition = parameters.first;
1467
+ // var success = parameters[1];
1468
+ // var failover = parameters[2];
1469
+ // return _concatIf(data, condition, success, failover);
1470
+ case 'filterOne':
1471
+ return _filterOne(data, parameters)
1472
+ case 'filterAll':
1473
+ return _filterAll(data, parameters)
1474
+ // case 'formatDate':
1475
+ // if (parameters is List<String>) {
1476
+ // return _formatDate(data, parameters);
1477
+ // }
1478
+ // throw const TemplateException(TemplateExceptionType.argumentFormatException);
1479
+ case 'eq':
1480
+ return _eq(data, parameters)
1481
+ // case 'neq':
1482
+ // return _neq(data, parameters);
1483
+ // case 'gt':
1484
+ // return _gt(data, parameters);
1485
+ // case 'gte':
1486
+ // return _gte(data, parameters);
1487
+ // case 'lt':
1488
+ // return _lt(data, parameters);
1489
+ // case 'lte':
1490
+ // return _lte(data, parameters);
1491
+ // case 'isEmpty':
1492
+ // return _isEmpty(data, parameters);
1493
+ // case 'isNotEmpty':
1494
+ // return _isNotEmpty(data, parameters);
1495
+ // case 'toLowerCase':
1496
+ // return _toLowerCase(data);
1497
+ // case 'toUpperCase':
1498
+ // return _toUpperCase(data);
1499
+ default:
1500
+ throw new Error(`${functionName} is not a valid function`)
1501
+ }
1502
+ }
1503
+
1504
+
1505
+
1506
+ ;// ./lib/models/template/index.js
1507
+
1508
+
1509
+
1510
+
1117
1511
  ;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
1118
1512
 
1119
1513
 
@@ -1171,6 +1565,7 @@ function _makeSetCode(fieldName, options) {
1171
1565
 
1172
1566
 
1173
1567
 
1568
+
1174
1569
  ;// ./lib/helpers/generalPost/generalPost.js
1175
1570
 
1176
1571
 
@@ -1370,6 +1765,7 @@ var __webpack_exports__Metadata = __webpack_exports__.OS;
1370
1765
  var __webpack_exports__QMeta = __webpack_exports__.Z8;
1371
1766
  var __webpack_exports__Repo = __webpack_exports__.lc;
1372
1767
  var __webpack_exports__Service = __webpack_exports__.kl;
1768
+ var __webpack_exports__Template = __webpack_exports__.Bj;
1373
1769
  var __webpack_exports__UniqueKeyGenerator = __webpack_exports__._x;
1374
1770
  var __webpack_exports__convertString = __webpack_exports__.l0;
1375
1771
  var __webpack_exports__formatDate = __webpack_exports__.Yq;
@@ -1383,4 +1779,4 @@ var __webpack_exports__pReduce = __webpack_exports__.d;
1383
1779
  var __webpack_exports__padZeros = __webpack_exports__.Lv;
1384
1780
  var __webpack_exports__stringFormatter = __webpack_exports__.Qy;
1385
1781
  var __webpack_exports__stringHelper = __webpack_exports__.yO;
1386
- 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 };
1782
+ 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.6",
3
+ "version": "0.1.8",
4
4
  "description": "Questwork QUtilities",
5
5
  "main": "dist/index.min.js",
6
6
  "type": "module",