Skip to content

Commit

Permalink
feat: add resolveModule util function
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Apr 7, 2022
1 parent a3804d3 commit 5955f4e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ console.log(sortedValue) // a, b or c

> Use Route to manipulate paths, getParams, getQueryParams, create route matcher RegExp etc.
```js
`````jsts`
import { Route } from '@secjs/utils'

const absolutePath = '/tests/:id/users/:user_id'
Expand All @@ -500,7 +500,7 @@ regExpMatcher.test(Route.removeQueryParams(path)) // true
> Use Blacklist to add, find and remove values from a blacklist/whitelist file
```js
`````jsts`
import { Blacklist } from '@secjs/utils'
const blacklist = new Blacklist()
Expand Down Expand Up @@ -532,7 +532,7 @@ await blacklist.remove('192.168.0.2', filePath) // void

> Use Number to manipulate numbers the best way
```js
`````jsts`
import { Number } from '@secjs/utils'

const arrayOfNumbers = [2, 4]
Expand Down Expand Up @@ -563,7 +563,7 @@ console.log(Number.randomIntFromInterval(1, 10)) // 8
> Generate UUID tokens using a prefix, and validate it to using uuidv4 lib
```js
`````jsts`
import { Token } from '@secjs/utils'
// Do not use the char "-", it would break token.verify() method
Expand Down Expand Up @@ -730,7 +730,7 @@ const connectionUrl = Parser.connectionObjToDbUrl(connectionObject)

> Use Clean to clean arrays and objects
```js
`````jsts`
import { Clean } from '@secjs/utils'

const array = [null, undefined, 1, "number"]
Expand Down Expand Up @@ -759,7 +759,7 @@ console.log(Clean.cleanArraysInObject(object2)) // { number2: [{ number1: "numbe
> Use Debug to generate debug logs in SecJS format
```js
`````jsts`
import { Debug } from '@secjs/utils'
const context = 'API'
Expand Down Expand Up @@ -797,7 +797,7 @@ import '@secjs/utils/src/Utils/global'

> Get the actual git branch that the project is running or not a repository.
```js
`````jsts`
import { getBranch } from '@secjs/utils'

await getBranch() // master || Not a repository
Expand All @@ -809,7 +809,7 @@ await getBranch() // master || Not a repository
> Get the actual commit id from the local repository.
```js
`````jsts`
import { getCommitId } from '@secjs/utils'
await getCommitId() // the commit sha || Not a repository
Expand All @@ -821,7 +821,7 @@ await getCommitId() // the commit sha || Not a repository

> Download an archive/image to determined path.
```js
`````jsts`
import { download } from '@secjs/utils'

;(async function () {
Expand All @@ -840,7 +840,7 @@ import { download } from '@secjs/utils'
> Use scheduler to execute some function based on MS
```js
`````jsts`
import { scheduler } from '@secjs/utils'
const func = () => {
Expand All @@ -856,7 +856,7 @@ scheduler(func, 3000) // scheduler function will execute the func every 3 second

> Use paginate get meta and links from for response
```js
`````jsts`
import { paginate, PaginationContract } from '@secjs/utils'

const filters = {
Expand Down Expand Up @@ -895,11 +895,27 @@ console.log(paginate(array, total, pagination))
---
### resolveModule
> Use resolveModule to resolve the module export type
```ts
import { resolveModule } from '@secjs/utils'
export function testExport() {}
const testExportFn = resolveModule(await import('./path/to/testExport'))
export default function testDefaultExport() {}
const testDefaultExportFn = resolveModule(await import('./path/to/testDefaultExport'))
```

---

### sleep

> Use sleep to let you code sleep for sometime
```js
```ts
import { sleep } from '@secjs/utils'

await sleep(2000) // Your code will stop in this line for two seconds
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from './src/Utils/paginate'
export * from './src/Utils/scheduler'
export * from './src/Utils/getBranch'
export * from './src/Utils/getCommitId'
export * from './src/Utils/resolveModule'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/utils",
"version": "1.8.2",
"version": "1.8.3",
"description": "",
"license": "MIT",
"author": "João Lenon <lenonSec7@gmail.com>",
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/resolveModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Is } from '../Helpers/Is'

export function resolveModule(module: any) {
if (Is.Class(module)) {
return module
}

if (Is.Object(module) && !module.default) {
const firstProviderKey = Object.keys(module)[0]

return module[firstProviderKey]
}

return module.default()
}

0 comments on commit 5955f4e

Please sign in to comment.