refactor utils

This commit is contained in:
wancat committed 2024-02-06 09:20:01 +08:00
1 parent 23f9023f8b
commit e91774fe3b
6 files changed
+93 -80

No files matched your search

@@ -1,4 +1,4 @@
export function isValidUsername(username) {
export function isValidUsername(username: string): boolean {
// check if the username is between 1 and 32 characters
if (username.length > 32) {
return false
@@ -29,7 +29,7 @@ export function isValidUsername(username) {
return true
}
export function isValidEmail(email) {
export function isValidEmail(email: string): boolean {
// Define a regular expression pattern for a valid email address.
const emailPattern = /^[\w-.+]+@([\w][\w-]*\.)+[\w-]{2,}$/
@@ -3,14 +3,21 @@ const fileTypes = {
folder: '-d',
link: '-L',
socket: '-S',
unknown: '-e',
}
function sanitizePath(path) {
export type FileType = keyof typeof fileTypes
export type File = {
path: string
type: FileType
}
function sanitizePath(path: string): string {
return path.replace(/[^a-zA-Z0-9._/-]/g, '')
}
// eslint-disable-next-line import/prefer-default-export
export function buildFileCheckCmd(file) {
export function buildFileCheckCmd(file: File): string[] {
const { path, type } = file
return ['test', fileTypes[type] || '-e', sanitizePath(path)]
}
-73
View File
@@ -1,73 +0,0 @@
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])
})
}
}
+76
View File
@@ -0,0 +1,76 @@
interface Node {
_id: string
requirements: string[]
}
export default class DAG {
private nodes = new Map()
private edges = new Set()
private layers: number[] = []
constructor(nodes: Node[]) {
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: string) {
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: string) {
const node = this.getNode(id)
if (!node) {
throw new Error(`Node ${id} not found`)
}
return this.layers[node.layer - 1]
}
private setLayer(id: string) {
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: string) => 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: string) {
const node = this.getNode(id)
if (!node) {
throw new Error(`Node ${id} not found`)
}
node.requirements.forEach((rId: string) => {
this.edges.add([rId, id])
})
}
}
File renamed without changes.
+5 -2
View File
@@ -1,8 +1,11 @@
import { buildFileCheckCmd } from '../src/commands'
import { buildFileCheckCmd, File } from '../src/commands'
describe('buildFileCheckCmd function', () => {
it('builds the file check command correctly', () => {
const testcases = [
const testcases: {
input: File
output: string[]
}[] = [
{
input: { path: '/etc/passwd', type: 'file' },
output: ['test', '-f', '/etc/passwd'],