Skip to content

Commit

Permalink
Add custom string index support
Browse files Browse the repository at this point in the history
- Added custom string index support
- Optimized calculating logical expressions with return-early pattern
- Fixed: return value only checked if the index was an array
  • Loading branch information
aliendrew committed Aug 3, 2023
1 parent 22f4507 commit 3b0d173
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 3b0d173

Please sign in to comment.