fix: setup Jest with TS

This commit is contained in:
Lin, Hung-Shin authored and wancat committed 2024-01-29 12:42:50 +08:00
1 parent 557af91d4b
commit a18e0d9b68
12 files changed
+45 -9

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
export function isValidUsername(username) {
// check if the username is between 1 and 32 characters
if (username.length > 32) {
return false
}
// Define a regular expression pattern for a valid Linux username.
const usernamePattern = /^[a-z][a-z0-9_-]*$/
// Test the username against the pattern.
if (!usernamePattern.test(username)) return false
const reservedWords = [
'root',
'admin',
'administrator',
'sysadmin',
'system',
'daemon',
'bin',
'nobody',
'nogroup',
'guest',
'user',
]
if (reservedWords.includes(username)) {
return false
}
return true
}
export function isValidEmail(email) {
// Define a regular expression pattern for a valid email address.
const emailPattern = /^[\w-.+]+@([\w][\w-]*\.)+[\w-]{2,}$/
// Test the email against the pattern.
return emailPattern.test(email)
}
+16
View File
@@ -0,0 +1,16 @@
const fileTypes = {
file: '-f',
folder: '-d',
link: '-L',
socket: '-S',
}
function sanitizePath(path) {
return path.replace(/[^a-zA-Z0-9._/-]/g, '')
}
// eslint-disable-next-line import/prefer-default-export
export function buildFileCheckCmd(file) {
const { path, type } = file
return ['test', fileTypes[type] || '-e', sanitizePath(path)]
}
+73
View File
@@ -0,0 +1,73 @@
export default class DAG {
#nodes = new Map()
#edges = new Set()
#layers = []
constructor(nodes) {
this.#nodes = new Map(nodes.map((node) => [node._id, node]))
this.#edges = new Set()
this.#nodes.forEach((_, id) => {
this.#setLayer(id)
this.#addEdges(id)
})
}
getNode(id) {
return this.#nodes.get(id)
}
getNodes() {
return Array.from(this.#nodes.values())
}
getEdges() {
return this.#edges
}
getEdgesArray() {
return Array.from(this.#edges.values())
}
getLayers() {
return this.#layers
}
getLayer(id) {
const node = this.getNode(id)
if (!node) {
throw new Error(`Node ${id} not found`)
}
return this.#layers[node.layer - 1]
}
#setLayer(id) {
const node = this.getNode(id)
if (!node) {
throw new Error(`Node ${id} not found`)
}
if (node.layer !== undefined) {
return node.layer
}
const previousLayer = node.requirements.map((rId) => this.#setLayer(rId))
node.layer = Math.max(...previousLayer, 0) + 1
if (this.#layers.length < node.layer) {
this.#layers.push(1)
} else {
this.#layers[node.layer - 1] += 1
}
node.index = this.#layers[node.layer - 1]
return node.layer
}
#addEdges(id) {
const node = this.getNode(id)
if (!node) {
throw new Error(`Node ${id} not found`)
}
node.requirements.forEach((rId) => {
this.#edges.add([rId, id])
})
}
}
+3
View File
@@ -0,0 +1,3 @@
export { isValidEmail, isValidUsername } from './auth.js'
export { buildFileCheckCmd } from './commands.js'
export { default as DAG } from './dag.js'