@platformatic/foundation 3.0.0-alpha.2

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.
package/lib/string.js ADDED
@@ -0,0 +1,95 @@
1
+ import leven from 'leven'
2
+
3
+ const UNITS = ['b', 'kb', 'mb', 'gb']
4
+ const UNITS_FACTOR = 1024
5
+
6
+ export function findNearestString (strings, target) {
7
+ let nearestString = null
8
+ let nearestDistance = Infinity
9
+
10
+ for (const string of strings) {
11
+ const distance = leven(string, target)
12
+ if (distance < nearestDistance) {
13
+ nearestString = string
14
+ nearestDistance = distance
15
+ }
16
+ }
17
+ return nearestString
18
+ }
19
+
20
+ export function match (actual, expected) {
21
+ if (typeof actual === 'string' && typeof expected === 'string') {
22
+ const patterns = expected
23
+ .trim()
24
+ .split(/\r?\n/)
25
+ .map(s => s.trim())
26
+
27
+ let lastIndex = -1
28
+ for (const pattern of patterns) {
29
+ const index = actual.indexOf(pattern)
30
+ if (index === -1 || index < lastIndex) {
31
+ return false
32
+ }
33
+ lastIndex = index
34
+ }
35
+ return true
36
+ }
37
+
38
+ for (const key in expected) {
39
+ if (key in actual) {
40
+ if (typeof expected[key] === 'object' && expected[key] !== null) {
41
+ /* c8 ignore next 3 */
42
+ if (!match(actual[key], expected[key])) {
43
+ return false
44
+ }
45
+ } else {
46
+ if (actual[key] !== expected[key]) {
47
+ return false
48
+ }
49
+ }
50
+ } else {
51
+ return false
52
+ }
53
+ }
54
+ return true
55
+ }
56
+
57
+ // Once we drop Node < 24, remove this in favor of Regexp.escape which is more accurate
58
+ export function escapeRegexp (raw) {
59
+ return raw.replaceAll(/([!$()*+./:=?[\\\]^{|}])/g, '\\$1')
60
+ }
61
+
62
+ /**
63
+ * Parse a memory size string and return the number of bytes
64
+ * @param {string} size - The memory size string
65
+ * @returns {number} The number of bytes
66
+ * @throws {Error} If the memory size string is invalid
67
+ * @example
68
+ * parseMemorySize('1024') // 1024
69
+ * parseMemorySize('1024B') // 1024
70
+ * parseMemorySize('1024b') // 1024
71
+ * parseMemorySize('1024KB') // 1048576
72
+ * parseMemorySize('1024kb') // 1048576
73
+ * parseMemorySize('1024gb') // 1099511627776
74
+ * parseMemorySize('1024MB') // 1073741824
75
+ * parseMemorySize('1024.2 MB') // 1073741824
76
+ * parseMemorySize('1024GB') // 1099511627776
77
+ * parseMemorySize('0.5 GB') // 536870912
78
+ * parseMemorySize('1024 GB') // 1099511627776
79
+ */
80
+ export function parseMemorySize (size) {
81
+ const match = size.match(/^(\d+\.?\d*)\s*([A-Za-z]+)$/)
82
+ if (!match) {
83
+ throw new Error('Invalid memory size')
84
+ }
85
+
86
+ const value = parseFloat(match[1])
87
+ const unit = match[2].toLowerCase()
88
+
89
+ const index = UNITS.indexOf(unit)
90
+ if (index === -1) {
91
+ throw new Error('Invalid memory size')
92
+ }
93
+
94
+ return Math.floor(value * UNITS_FACTOR ** index)
95
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@platformatic/foundation",
3
+ "version": "3.0.0-alpha.2",
4
+ "description": "Platformatic Foundation",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/platformatic/platformatic.git"
11
+ },
12
+ "author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",
13
+ "license": "Apache-2.0",
14
+ "bugs": {
15
+ "url": "https://github.com/platformatic/platformatic/issues"
16
+ },
17
+ "homepage": "https://github.com/platformatic/platformatic#readme",
18
+ "dependencies": {
19
+ "@fastify/error": "^4.0.0",
20
+ "@iarna/toml": "^2.2.5",
21
+ "@watchable/unpromise": "^1.0.2",
22
+ "ajv": "^8.12.0",
23
+ "boring-name-generator": "^1.0.3",
24
+ "colorette": "^2.0.19",
25
+ "dotenv": "^16.4.5",
26
+ "fast-json-patch": "^3.1.1",
27
+ "json5": "^2.2.3",
28
+ "leven": "~3.1.0",
29
+ "pino": "^9.9.0",
30
+ "pino-pretty": "^13.0.0",
31
+ "semver": "^7.6.3",
32
+ "undici": "7.11.0",
33
+ "yaml": "^2.4.1"
34
+ },
35
+ "devDependencies": {
36
+ "borp": "^0.20.0",
37
+ "c8": "^10.0.0",
38
+ "eslint": "9",
39
+ "fastify": "^5.0.0",
40
+ "neostandard": "^0.12.0",
41
+ "pino": "^9.9.0",
42
+ "pino-test": "^1.0.1",
43
+ "typescript": "^5.5.4"
44
+ },
45
+ "engines": {
46
+ "node": ">=22.18.0"
47
+ },
48
+ "scripts": {
49
+ "test": "npm run lint && node --test --test-concurrency=1 --test-timeout=1200000 test/*.test.js",
50
+ "coverage": "c8 -r html -r text node --test --test-concurrency=1 --test-timeout=1200000 test/*.test.js",
51
+ "lint": "eslint"
52
+ }
53
+ }