Skip to content

Commit c5c6999

Browse files
authored
Merge pull request #25 from SecJS/feat/len-add-unset-fn
feat: Add File and Folder class
2 parents 5ae8706 + 9797f09 commit c5c6999

27 files changed

+2127
-305
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,9 @@ out
126126
*.d.ts
127127
dist
128128
build
129+
130+
# testing files generated here
131+
file-class-test
132+
file-class-global-test
133+
folder-class-test
134+
folder-class-global-test

README.md

Lines changed: 121 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The intention behind this repository is to always maintain a `Utils` package wit
2929
> it keeps as dev dependency because one day `@secjs/core` will install everything once.
3030
3131
```bash
32-
npm install @secjs/contracts @secjs/exceptions
32+
npm install @secjs/logger @secjs/contracts @secjs/exceptions
3333
```
3434

3535
> Then you can install the package using:
@@ -40,6 +40,100 @@ npm install @secjs/utils
4040

4141
## Classes Usage
4242

43+
### File
44+
45+
> Use File to create an instance of a File, it's existing or not.
46+
47+
```ts
48+
import { File } from '@secjs/utils'
49+
50+
// With file you can manipulate an existing file, or create a new one
51+
52+
const existentFile = new File('path/to/existent/file.txt')
53+
const nonExistentFile = new File('path/to/nonExistent/file.txt', Buffer.from('File content'))
54+
55+
// Now existentFile and nonExistentFile instances are created, but not loaded/created
56+
57+
// using load here because the file already exists, if using create, would generate an exception
58+
// property withContent if true, will save the file content in the instance, Be careful with big files
59+
existentFile.loadSync({ withContent: true })
60+
nonExistentFile.createSync().loadSync({ withContent: true })
61+
62+
// now the files will have this properties
63+
console.log(existentFile.createdAt)
64+
console.log(existentFile.accessedAt)
65+
console.log(existentFile.modifiedAt)
66+
console.log(existentFile.fileSize)
67+
console.log(existentFile.content)
68+
69+
// you can delete the file using remove method
70+
existentFile.removeSync() // void
71+
72+
// you can get the content of the file with getContent method
73+
console.log(existentFile.getContentSync()) // Some Buffer instance
74+
75+
// you can use toJSON method to get the instance informations in JSON
76+
console.log(existentFile.toJSON()) // { ...infos }
77+
78+
// File uses readable streams in async methods to not block the event loop when handling huge files content
79+
await existentFile.load()
80+
await existentFile.remove()
81+
await existentFile.create()
82+
await existentFile.getContent()
83+
```
84+
85+
---
86+
87+
### Folder
88+
89+
> Use Folder to create an instance of a Folder, it's existing or not.
90+
91+
```ts
92+
import { Folder } from '@secjs/utils'
93+
94+
// With folder you can manipulate an existing folder, or create a new one
95+
96+
const existentFolder = new Folder('path/to/existent/folder')
97+
const nonExistentFolder = new Folder('path/to/nonExistent/folder')
98+
99+
// Now existentFolder and nonExistentFolder instances are created, but not loaded/created
100+
101+
// using load here because the file already exists, if using create, would generate an exception
102+
103+
// property withSub if true, will load files and subFolders from the folder
104+
// property withFileContent if true, will get the content of all files in the folder, Be careful with big files
105+
106+
existentFolder.loadSync({ withSub: true, withFileContent: false })
107+
nonExistentFolder.createSync().loadSync({ withSub: true, withFileContent: true })
108+
109+
// now the folders will have this properties
110+
console.log(existentFolder.createdAt)
111+
console.log(existentFolder.accessedAt)
112+
console.log(existentFolder.modifiedAt)
113+
console.log(existentFolder.folderSize)
114+
115+
// you can delete the folder using remove method
116+
existentFolder.removeSync() // void
117+
118+
// you can use toJSON method to get the instance informations in JSON
119+
console.log(existentFolder.toJSON()) // { ...infos }
120+
121+
// you can use getFilesByPattern method to get all files in the folder that match some pattern
122+
// if recursive is true, will go inside subFolders too
123+
const recursive = true
124+
console.log(existentFolder.getFilesByPattern('path/to/**/*.ts', recursive)) // [...files instance]
125+
126+
// you can use getFoldersByPattern method to get all folders in the folder that match some pattern
127+
console.log(existentFolder.getFoldersByPattern('path/to/**/folder', recursive)) // [...folders instance]
128+
129+
// Folder uses readable streams in async methods to not block the event loop when handling huge files content
130+
await existentFolder.load()
131+
await existentFolder.remove()
132+
await existentFolder.create()
133+
```
134+
135+
---
136+
43137
### Path
44138

45139
> Use Path to get the absolute path from project folders.
@@ -282,6 +376,21 @@ console.log(clean.cleanArraysInObject(object2)) // { number2: [{ number1: "numbe
282376

283377
## Functions Usage
284378

379+
### formatBytes
380+
381+
> Creates a string based on the bytes size.
382+
383+
```ts
384+
import { formatBytes } from '@secjs/utils'
385+
386+
const bytes = 1024*1024*1024 // 1GB
387+
const decimals = 4
388+
389+
formatBytes(bytes, decimals) // Example: 1.0932 GB
390+
```
391+
392+
---
393+
285394
### getBranch
286395

287396
> Get the actual git branch that the project is running or not a repository.
@@ -292,28 +401,7 @@ import { getBranch } from '@secjs/utils'
292401
await getBranch() // master || Not a repository
293402
```
294403

295-
### pathPattern
296-
297-
> Transform all route paths to the same pattern.
298-
299-
```js
300-
import { pathPattern } from '@secjs/utils'
301-
302-
pathPattern('/users/:id/') // returns /users/:id
303-
pathPattern('clients/') // returns /clients
304-
pathPattern('/api/v2') // returns /api/v2
305-
pathPattern('/api/v3/') // returns /api/v3
306-
307-
pathPattern(['/api/v1/', 'api/v2', 'api/v3/', '/api/v4'])
308-
309-
// returns
310-
// [
311-
// '/api/v1',
312-
// '/api/v2',
313-
// '/api/v3',
314-
// '/api/v4'
315-
// ]
316-
```
404+
---
317405

318406
### download
319407

@@ -334,53 +422,6 @@ import { download } from '@secjs/utils'
334422

335423
---
336424

337-
### getFiles
338-
339-
> Get all files inside a path and files inside folders if needed
340-
341-
```js
342-
import { getFiles } from '@secjs/utils'
343-
344-
const iterateFolders = false
345-
for await (const file of getFiles('any/path', iterateFolders)) {
346-
console.log(file) // /home/path/to/your/file
347-
}
348-
```
349-
350-
### getFolders
351-
352-
> Get all folders inside a path and files if needed.
353-
354-
```js
355-
import { getFolders } from '@secjs/utils'
356-
357-
const withFiles = true
358-
const directory = await getFolders('some/path', withFiles)
359-
360-
// {
361-
// path: '/home/some/path',
362-
// files: ['/home/some/path/file.ts'],
363-
// folders: [{
364-
// path: '/home/some/path/folder',
365-
// files: ['/home/some/path/file.ts'],
366-
// folders: []
367-
// }] as IDirectory[]
368-
// } as IDirectory
369-
```
370-
371-
### fileExists
372-
373-
> Return true if file exists or false
374-
375-
```js
376-
import { fileExists } from '@secjs/utils'
377-
378-
// Just abstracting the error that node throws
379-
// if file does not exist
380-
381-
console.log(fileExists('path/to/file')) // true or false
382-
```
383-
384425
### observeChanges
385426

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

449+
---
450+
408451
### removeDuplicated
409452

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

463+
---
464+
420465
### randomColor
421466

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

475+
---
476+
430477
### isArrayOfObjects
431478

432-
> Use isArrayOfObjects to verify if all values inside of the array are objects
479+
> Use isArrayOfObjects to verify if all values inside the array are objects
433480
434481
```js
435482
import { isArrayOfObjects } from '@secjs/utils'
@@ -447,9 +494,11 @@ console.log(isArrayOfObjects(array3)) // true
447494
console.log(isArrayOfObjects(fakeArray)) // false
448495
```
449496

497+
---
498+
450499
### urlify
451500

452-
> Use urlify to inject some URL of a string inside a HTML Link
501+
> Use urlify to inject some URL of a string inside an HTML Link
453502
454503
```js
455504
import { urlify } from '@secjs/utils'

index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
export * from './src/Classes/Json'
22
export * from './src/Classes/Path'
3+
export * from './src/Classes/File'
34
export * from './src/Classes/Token'
45
export * from './src/Classes/Clean'
56
export * from './src/Classes/Route'
7+
export * from './src/Classes/Folder'
68
export * from './src/Classes/Parser'
79
export * from './src/Classes/Numbers'
810
export * from './src/Classes/Blacklist'
911

1012
export * from './src/Functions/sort'
13+
export * from './src/Functions/unset'
1114
export * from './src/Functions/sleep'
1215
export * from './src/Functions/isCpf'
1316
export * from './src/Functions/isCnpj'
@@ -17,12 +20,8 @@ export * from './src/Functions/download'
1720
export * from './src/Functions/fillable'
1821
export * from './src/Functions/kmRadius'
1922
export * from './src/Functions/paginate'
20-
export * from './src/Functions/getFiles'
2123
export * from './src/Functions/scheduler'
2224
export * from './src/Functions/getBranch'
23-
export * from './src/Functions/getFolders'
24-
export * from './src/Functions/fileExists'
25-
export * from './src/Functions/pathPattern'
2625
export * from './src/Functions/getCommitId'
2726
export * from './src/Functions/randomColor'
2827
export * from './src/Functions/observeChanges'

0 commit comments

Comments
 (0)