@cepharum/concrete-db 0.1.1 → 0.1.2-alpha.1

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,31 +1,3 @@
1
- /**
2
- * (c) 2021 cepharum GmbH, Berlin, http://cepharum.de
3
- *
4
- * The MIT License (MIT)
5
- *
6
- * Copyright (c) 2021 cepharum GmbH
7
- *
8
- * Permission is hereby granted, free of charge, to any person obtaining a copy
9
- * of this software and associated documentation files (the "Software"), to deal
10
- * in the Software without restriction, including without limitation the rights
11
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
- * copies of the Software, and to permit persons to whom the Software is
13
- * furnished to do so, subject to the following conditions:
14
- *
15
- * The above copyright notice and this permission notice shall be included in all
16
- * copies or substantial portions of the Software.
17
- *
18
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- * SOFTWARE.
25
- *
26
- * @author: cepharum
27
- */
28
-
29
1
  import { ForeignKey } from "./helper.mjs";
30
2
 
31
3
  /**
@@ -82,3 +54,76 @@ export function icompare( left, right ) {
82
54
  export function compare( left, right ) {
83
55
  return String( left ).localeCompare( String( right ) );
84
56
  }
57
+
58
+ /**
59
+ * Extracts list of terms from provided scalar or non-scalar value.
60
+ *
61
+ * A term is a consecutive sequence of letters and digits. On setting custom
62
+ * option for ignoring case, all letters of collected terms are converted to
63
+ * lowercase in resulting map.
64
+ *
65
+ * @param {any} value value to extract terms from
66
+ * @param {number} minSize minimum number of characters in a string to be considered for extracting terms
67
+ * @param {number} minTermSize minimum number of characters in extracted terms
68
+ * @param {number} maxTermSize maximum number of characters in extracted terms
69
+ * @param {boolean} ignoreCase set true to drop case of extracted terms
70
+ * @param {boolean} strict set false to have any non-string value converted to string instead of being ignored
71
+ * @returns {Object<string,number>} map of extracted terms into either term's number of occurrences
72
+ */
73
+ export function terms( value, minSize = 10, minTermSize = 1, maxTermSize = Infinity, ignoreCase = false, strict = true ) {
74
+ const result = {};
75
+
76
+ for ( const term of generateTerms( value, minSize, ignoreCase, strict ) ) {
77
+ if ( term.length >= minTermSize && term.length <= maxTermSize ) {
78
+ result[term] = ( result[term] || 0 ) + 1;
79
+ }
80
+ }
81
+
82
+ return result;
83
+ }
84
+
85
+
86
+ /**
87
+ * Recursively iterates over provided source converting all scalar data into
88
+ * string prior to yielding consecutive sequences of letters and digits.
89
+ *
90
+ * @param {any} source data to extract terms from
91
+ * @param {number} minSize minimum number of characters in a string to be considered for extracting terms
92
+ * @param {boolean} ignoreCase set true to drop case of extracted terms
93
+ * @param {boolean} strict set false to have any non-string value converted to string instead of being ignored
94
+ * @returns {Generator<string|*, void, *>} iterator over flat sequence of terms
95
+ */
96
+ function *generateTerms( source, minSize, ignoreCase, strict ) {
97
+ let stream;
98
+
99
+ if ( Array.isArray( source ) ) {
100
+ stream = source;
101
+ } else if ( source instanceof Set || source instanceof Map ) {
102
+ stream = source.values();
103
+ } else if ( source && typeof source === "object" ) {
104
+ stream = Object.values( source );
105
+ } else {
106
+ if ( strict && typeof source !== "string" ) {
107
+ return;
108
+ }
109
+
110
+ const string = String( source ).trim();
111
+
112
+ if ( string.length < minSize ) {
113
+ return;
114
+ }
115
+
116
+ const ptn = /([\p{L}\p{N}_][\p{L}\p{N}_-]*)/gu;
117
+ let match;
118
+
119
+ while ( ( match = ptn.exec( string ) ) ) {
120
+ yield ignoreCase ? match[1].toLocaleLowerCase() : match[1];
121
+ }
122
+
123
+ return;
124
+ }
125
+
126
+ for ( const sub of stream ) {
127
+ yield* generateTerms( sub, minSize, ignoreCase, strict );
128
+ }
129
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cepharum/concrete-db",
3
- "version": "0.1.1",
3
+ "version": "0.1.2-alpha.1",
4
4
  "description": "a read-only web database generator",
5
5
  "main": "lib/collector.mjs",
6
6
  "types": "concrete-db.d.ts",
@@ -10,7 +10,8 @@
10
10
  "scripts": {
11
11
  "cure": "node ./cure.mjs",
12
12
  "lint": "eslint lib/**/*.{js,mjs,cjs} test/**/*.{js,mjs,cjs} cure.mjs",
13
- "test": "nyc mocha --ui=tdd --recursive test/**/*.spec.*",
13
+ "lint:fix": "eslint --fix lib/**/*.{js,mjs,cjs} test/**/*.{js,mjs,cjs} cure.mjs",
14
+ "test": "c8 mocha --ui=tdd --recursive test/**/*.spec.*",
14
15
  "test:getting-started": "node ./test/curing.shell.mjs",
15
16
  "docs:dev": "vuepress dev docs",
16
17
  "docs:build": "vuepress build docs"
@@ -19,23 +20,26 @@
19
20
  "author": "cepharum GmbH",
20
21
  "license": "MIT",
21
22
  "dependencies": {
22
- "ajv": "^6.12.6",
23
+ "ajv": "^8.11.0",
23
24
  "file-essentials": "^0.1.2",
24
25
  "lodash.merge": "^4.6.2",
25
- "minimist": "^1.2.5",
26
+ "minimist": "^1.2.6",
26
27
  "promise-essentials": "^0.2.0",
27
- "simple-terms": "^0.3.0",
28
- "yaml": "^1.10.2"
28
+ "simple-terms": "^0.3.3",
29
+ "yaml": "^2.1.0"
29
30
  },
30
31
  "devDependencies": {
31
- "eslint": "^7.24.0",
32
+ "c8": "^7.11.3",
33
+ "eslint": "^8.16.0",
32
34
  "eslint-config-cepharum": "^1.0.12",
33
- "eslint-plugin-promise": "^5.1.0",
34
- "mocha": "^8.3.2",
35
- "nyc": "^15.1.0",
35
+ "eslint-plugin-promise": "^6.0.0",
36
+ "mocha": "^10.0.0",
36
37
  "should": "^13.2.3",
37
- "vuepress": "^1.8.2",
38
- "vuepress-plugin-mermaidjs": "^1.8.1"
38
+ "vuepress": "^1.9.7",
39
+ "vuepress-plugin-mermaidjs": "^1.9.1"
40
+ },
41
+ "engines": {
42
+ "node": ">=10"
39
43
  },
40
44
  "files": [
41
45
  "lib",