From 22f450740d507e57a25038d92d3092c29c3ebe9e Mon Sep 17 00:00:00 2001 From: Andrey Puzankov Date: Thu, 3 Aug 2023 16:24:05 +0800 Subject: [PATCH 1/3] Add custom string index support --- lib/metadata.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/metadata.js b/lib/metadata.js index d02e4a1..bb59a8c 100644 --- a/lib/metadata.js +++ b/lib/metadata.js @@ -62,7 +62,13 @@ class Indexes { extract(key, field) { const { index, primary, unique, many } = field; const isIndex = Array.isArray(index || primary || unique); - if (isIndex || many) this[key] = field; + const isStringIndex = + typeof index === 'string' && + index.indexOf('(') !== -1 && + index.lastIndexOf(')') !== -1 && + index.substring(index.indexOf('('), index.lastIndexOf(')')).trim() + .length !== 0; + if (isIndex || isStringIndex || many) this[key] = field; return isIndex; } } From 3b0d17322cc457aee474e3bc869be7ef14a54ed1 Mon Sep 17 00:00:00 2001 From: Andrey Puzankov Date: Thu, 3 Aug 2023 18:08:56 +0800 Subject: [PATCH 2/3] Add custom string index support - Added custom string index support - Optimized calculating logical expressions with return-early pattern - Fixed: return value only checked if the index was an array --- lib/metadata.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/metadata.js b/lib/metadata.js index bb59a8c..dc9bcf5 100644 --- a/lib/metadata.js +++ b/lib/metadata.js @@ -61,14 +61,18 @@ class Options { class Indexes { extract(key, field) { const { index, primary, unique, many } = field; - const isIndex = Array.isArray(index || primary || unique); - const isStringIndex = - typeof index === 'string' && - index.indexOf('(') !== -1 && - index.lastIndexOf(')') !== -1 && - index.substring(index.indexOf('('), index.lastIndexOf(')')).trim() - .length !== 0; - if (isIndex || isStringIndex || many) this[key] = field; + const chosenIndex = index || primary || unique; + const isStringIndex = (index) => { + if (typeof index !== 'string') return false; + if (index.indexOf('(') === -1 && index.indexOf(')') === -1) return false; + const expression = index.substring( + index.indexOf('(') + 1, + index.lastIndexOf(')'), + ); + return expression.trim().length !== 0; + }; + const isIndex = Array.isArray(chosenIndex) || isStringIndex(chosenIndex); + if (isIndex || many) this[key] = field; return isIndex; } } From 5bac2a3a99dbebe93c8a3bd5edd27a5d6f3ad239 Mon Sep 17 00:00:00 2001 From: Andrey Puzankov Date: Fri, 11 Aug 2023 00:50:38 +0800 Subject: [PATCH 3/3] Add custom string index support String index check moved to util.js --- lib/metadata.js | 10 +--------- lib/util.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/metadata.js b/lib/metadata.js index dc9bcf5..efadfe7 100644 --- a/lib/metadata.js +++ b/lib/metadata.js @@ -1,6 +1,7 @@ 'use strict'; const { getKindMetadata } = require('./kinds.js'); +const { isStringIndex } = require('./util.js'); const errPrefix = 'Field'; const OPTIONS = ['validate', 'parse', 'serialize', 'format']; @@ -62,15 +63,6 @@ class Indexes { extract(key, field) { const { index, primary, unique, many } = field; const chosenIndex = index || primary || unique; - const isStringIndex = (index) => { - if (typeof index !== 'string') return false; - if (index.indexOf('(') === -1 && index.indexOf(')') === -1) return false; - const expression = index.substring( - index.indexOf('(') + 1, - index.lastIndexOf(')'), - ); - return expression.trim().length !== 0; - }; const isIndex = Array.isArray(chosenIndex) || isStringIndex(chosenIndex); if (isIndex || many) this[key] = field; return isIndex; diff --git a/lib/util.js b/lib/util.js index 84b09e9..1b529e6 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,6 @@ 'use strict'; -const { isFirstLetter } = require('metautil'); +const { isFirstLetter, validateBracketBalance } = require('metautil'); const formatters = { type: (type, req = true) => { @@ -41,9 +41,22 @@ const firstKey = (obj) => Object.keys(obj).find(isFirstLetter); const isInstanceOf = (obj, constrName) => obj?.constructor?.name === constrName; +const isStringIndex = (index) => { + if (typeof index !== 'string') return false; + const brackets = ['(', ')']; + if (brackets.some((bracket) => index.indexOf(bracket) === -1)) return false; + if (!validateBracketBalance(index, brackets.join(''))) return false; + const expression = index.substring( + index.indexOf(brackets[0]) + 1, + index.lastIndexOf(brackets[1]), + ); + return expression.trim().length !== 0; +}; + module.exports = { formatters, checks, firstKey, isInstanceOf, + isStringIndex, };