Skip to content

Commit

Permalink
feat: Remove useless functions and add global tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Nov 24, 2021
1 parent 9e94f5a commit 6da3fcf
Show file tree
Hide file tree
Showing 27 changed files with 636 additions and 418 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@ build

# testing files generated here
file-class-test
file-class-global-test
folder-class-test
folder-class-global-test
125 changes: 12 additions & 113 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,48 +389,7 @@ const decimals = 4
formatBytes(bytes, decimals) // Example: 1.0932 GB
```

### createFileOfSize

> Create a file in determined size. Good for testing.
```ts
import { createFileOfSize } from '@secjs/utils'

createFileOfSize('path/to/file', 1024*1024*1024)
```

### dirSize

> Get the size of the directory without recursive going inside sub folders.
```ts
import { dirSize } from '@secjs/utils'

dirSize('path/to/directory') // The size in bytes
```

### pathPattern

> Transform all route paths to the same pattern.
```js
import { pathPattern } from '@secjs/utils'

pathPattern('/users/:id/') // returns /users/:id
pathPattern('clients/') // returns /clients
pathPattern('/api/v2') // returns /api/v2
pathPattern('/api/v3/') // returns /api/v3

pathPattern(['/api/v1/', 'api/v2', 'api/v3/', '/api/v4'])

// returns
// [
// '/api/v1',
// '/api/v2',
// '/api/v3',
// '/api/v4'
// ]
```
---

### getBranch

Expand All @@ -442,28 +401,7 @@ import { getBranch } from '@secjs/utils'
await getBranch() // master || Not a repository
```

### pathPattern

> Transform all route paths to the same pattern.
```js
import { pathPattern } from '@secjs/utils'

pathPattern('/users/:id/') // returns /users/:id
pathPattern('clients/') // returns /clients
pathPattern('/api/v2') // returns /api/v2
pathPattern('/api/v3/') // returns /api/v3

pathPattern(['/api/v1/', 'api/v2', 'api/v3/', '/api/v4'])

// returns
// [
// '/api/v1',
// '/api/v2',
// '/api/v3',
// '/api/v4'
// ]
```
---

### download

Expand All @@ -484,53 +422,6 @@ import { download } from '@secjs/utils'

---

### getFiles

> Get all files inside a path and files inside folders if needed
```js
import { getFiles } from '@secjs/utils'

const iterateFolders = false
for await (const file of getFiles('any/path', iterateFolders)) {
console.log(file) // /home/path/to/your/file
}
```

### getFolders

> Get all folders inside a path and files if needed.
```js
import { getFolders } from '@secjs/utils'

const withFiles = true
const directory = await getFolders('some/path', withFiles)

// {
// path: '/home/some/path',
// files: ['/home/some/path/file.ts'],
// folders: [{
// path: '/home/some/path/folder',
// files: ['/home/some/path/file.ts'],
// folders: []
// }] as IDirectory[]
// } as IDirectory
```

### fileExists

> Return true if file exists or false
```js
import { fileExists } from '@secjs/utils'

// Just abstracting the error that node throws
// if file does not exist

console.log(fileExists('path/to/file')) // true or false
```

### observeChanges

> Use observeChanges to observe changes in the value of an object
Expand All @@ -555,6 +446,8 @@ data.name = 'João'
// Name changed to: João { value: 'args are the same second parameter of doSomething function' }
```

---

### removeDuplicated

> Use removeDuplicated to remove duplicated values from an Array
Expand All @@ -567,6 +460,8 @@ const array = [1, 1, 2, 4, 4]
console.log(removeDuplicated(array)) // [1, 2, 4]
```

---

### randomColor

> Use randomColor to generate a random Hexadecimal color
Expand All @@ -577,9 +472,11 @@ import { randomColor } from '@secjs/utils'
console.log(randomColor()) // #7059c1
```

---

### isArrayOfObjects

> Use isArrayOfObjects to verify if all values inside of the array are objects
> Use isArrayOfObjects to verify if all values inside the array are objects
```js
import { isArrayOfObjects } from '@secjs/utils'
Expand All @@ -597,9 +494,11 @@ console.log(isArrayOfObjects(array3)) // true
console.log(isArrayOfObjects(fakeArray)) // false
```

---

### urlify

> Use urlify to inject some URL of a string inside a HTML Link
> Use urlify to inject some URL of a string inside an HTML Link
```js
import { urlify } from '@secjs/utils'
Expand Down
5 changes: 0 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ export * from './src/Functions/download'
export * from './src/Functions/fillable'
export * from './src/Functions/kmRadius'
export * from './src/Functions/paginate'
export * from './src/Functions/getFiles'
export * from './src/Functions/scheduler'
export * from './src/Functions/getBranch'
export * from './src/Functions/getFolders'
export * from './src/Functions/fileExists'
export * from './src/Functions/pathPattern'
export * from './src/Functions/getCommitId'
export * from './src/Functions/randomColor'
export * from './src/Functions/observeChanges'
export * from './src/Functions/removeDuplicated'
export * from './src/Functions/isArrayOfObjects'
export * from './src/Functions/createFileOfSize'
35 changes: 26 additions & 9 deletions src/Classes/File.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* @secjs/utils
*
* (c) João Lenon <lenon@secjs.com.br>
Expand Down Expand Up @@ -48,13 +48,28 @@ export interface FileJsonContract {
}

export class File {
constructor(path: string, content: Buffer | null = null) {
const { ext, dir, name, base, mime } = File.parsePath(path)
static async createFileOfSize(filePath: string, size: number) {
const { dir, path } = File.parsePath(filePath)

await promises.mkdir(dir, { recursive: true })

return new Promise((resolve, reject) => {
const writable = createWriteStream(path)

writable.write(Buffer.alloc(Math.max(0, size - 2), 'l'))

writable.end(() => resolve(this))
writable.on('error', err => reject(err))
})
}

constructor(filePath: string, content: Buffer | null = null) {
const { ext, dir, name, base, mime, path } = File.parsePath(filePath)

this._originalDir = dir
this._originalName = name
this._originalBase = base
this._originalPath = this._originalDir + '/' + this._originalBase
this._originalPath = path
this._originalFileExists = existsSync(this._originalPath)
this._fileExists = this._originalFileExists
this._content = content
Expand Down Expand Up @@ -130,7 +145,7 @@ export class File {
})
}

loadSync(options?: { withContent: boolean }) {
loadSync(options?: { withContent?: boolean }) {
options = Object.assign({}, { withContent: true }, options)

if (this._content && this._fileExists)
Expand Down Expand Up @@ -162,7 +177,7 @@ export class File {
return this
}

async load(options?: { withContent: boolean }): Promise<File> {
async load(options?: { withContent?: boolean }): Promise<File> {
options = Object.assign({}, { withContent: true }, options)

if (this._content && this._fileExists) {
Expand Down Expand Up @@ -287,8 +302,10 @@ export class File {
this._path = this._dir + '/' + this._base
}

private static parsePath(path: string) {
const { base, dir, root } = parse(isAbsolute(path) ? path : Path.pwd(path))
private static parsePath(filePath: string) {
const { base, dir, root } = parse(
isAbsolute(filePath) ? filePath : Path.pwd(filePath),
)

const baseArray = base.split('.')

Expand All @@ -300,7 +317,7 @@ export class File {
}, '')
const mime = lookup(dir + '/' + base)

return { ext, dir, name, root, base, mime }
return { ext, dir, name, root, base, mime, path: dir + '/' + base }
}

private _dir?: string
Expand Down
28 changes: 20 additions & 8 deletions src/Classes/Folder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* @secjs/utils
*
* (c) João Lenon <lenon@secjs.com.br>
Expand All @@ -22,7 +22,7 @@ import {
import { File } from './File'
import { Path } from './Path'
import { randomBytes } from 'crypto'
import { isAbsolute, parse, resolve } from 'path'
import { isAbsolute, join, parse, resolve } from 'path'
import { formatBytes } from '../Functions/formatBytes'
import { InternalServerException } from '@secjs/exceptions'

Expand All @@ -45,12 +45,22 @@ export interface FolderJsonContract {
}

export class Folder {
constructor(path: string) {
const { dir, name } = Folder.parsePath(path)
static async folderSize(folderPath: string): Promise<number> {
const files = await promises.readdir(folderPath)
const stats = files.map(file => promises.stat(join(folderPath, file)))

return (await Promise.all(stats)).reduce(
(accumulator, { size }) => accumulator + size,
0,
)
}

constructor(folderPath: string) {
const { dir, name, path } = Folder.parsePath(folderPath)

this._originalDir = dir
this._originalName = name
this._originalPath = this._originalDir + '/' + this._originalName
this._originalPath = path
this._originalFolderExists = existsSync(this._originalPath)
this._folderExists = this._originalFolderExists

Expand Down Expand Up @@ -298,10 +308,12 @@ export class Folder {
this._path = this._dir + '/' + this._name
}

private static parsePath(path: string) {
const { dir, name } = parse(isAbsolute(path) ? path : Path.pwd(path))
private static parsePath(folderPath: string) {
const { dir, name } = parse(
isAbsolute(folderPath) ? folderPath : Path.pwd(folderPath),
)

return { dir, name }
return { dir, name, path: dir + '/' + name }
}

private loadSubSync(
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/Path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* @secjs/utils
*
* (c) João Lenon <lenon@secjs.com.br>
Expand Down
33 changes: 0 additions & 33 deletions src/Functions/createFileOfSize.ts

This file was deleted.

Loading

0 comments on commit 6da3fcf

Please sign in to comment.