├── .editorconfig
├── .github
└── workflows
│ ├── checks.yml
│ ├── labels.yml
│ ├── release.yml
│ └── stale.yml
├── .gitignore
├── .npmrc
├── .prettierignore
├── LICENSE.md
├── README.md
├── bin
└── test.ts
├── eslint.config.js
├── index.ts
├── lodash
└── lodash.types.d.ts
├── package.json
├── src
├── assert.ts
├── base64.ts
├── compose.ts
├── define_static_property.ts
├── exception.ts
├── exceptions
│ └── main.ts
├── flatten.ts
├── fs_import_all.ts
├── fs_read_all.ts
├── import_default.ts
├── is_script_file.ts
├── json
│ ├── main.ts
│ ├── safe_parse.ts
│ └── safe_stringify.ts
├── message_builder.ts
├── natural_sort.ts
├── object_builder.ts
├── safe_equal.ts
├── secret.ts
├── slash.ts
├── string
│ ├── main.ts
│ └── string_builder.ts
└── types.ts
├── test_helpers
└── index.ts
├── tests
├── assert.spec.ts
├── base64.spec.ts
├── compose.spec.ts
├── define_static_property.spec.ts
├── flatten.spec.ts
├── fs_import_all.spec.ts
├── fs_read_all.spec.ts
├── import_default.spec.ts
├── lodash.spec.ts
├── message_builder.spec.ts
├── safe_equal.spec.ts
├── safe_parse.spec.ts
├── safe_stringify.spec.ts
├── secret.spec.ts
└── slash.spec.ts
└── tsconfig.json
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 2
6 | end_of_line = lf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = true
10 |
11 | [*.json]
12 | insert_final_newline = ignore
13 |
14 | [**.min.js]
15 | indent_style = ignore
16 | insert_final_newline = ignore
17 |
18 | [MakeFile]
19 | indent_style = space
20 |
21 | [*.md]
22 | trim_trailing_whitespace = false
23 |
--------------------------------------------------------------------------------
/.github/workflows/checks.yml:
--------------------------------------------------------------------------------
1 | name: checks
2 | on:
3 | - push
4 | - pull_request
5 | - workflow_call
6 |
7 | jobs:
8 | test:
9 | uses: poppinss/.github/.github/workflows/test.yml@main
10 |
11 | lint:
12 | uses: poppinss/.github/.github/workflows/lint.yml@main
13 |
14 | typecheck:
15 | uses: poppinss/.github/.github/workflows/typecheck.yml@main
16 |
--------------------------------------------------------------------------------
/.github/workflows/labels.yml:
--------------------------------------------------------------------------------
1 | name: Sync labels
2 | on:
3 | workflow_dispatch:
4 | permissions:
5 | issues: write
6 | jobs:
7 | labels:
8 | runs-on: ubuntu-latest
9 | steps:
10 | - uses: actions/checkout@v4
11 | - uses: EndBug/label-sync@v2
12 | with:
13 | config-file: 'https://raw.githubusercontent.com/thetutlage/static/main/labels.yml'
14 | delete-other-labels: true
15 | token: ${{ secrets.GITHUB_TOKEN }}
16 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: release
2 | on: workflow_dispatch
3 | permissions:
4 | contents: write
5 | id-token: write
6 | jobs:
7 | checks:
8 | uses: ./.github/workflows/checks.yml
9 | release:
10 | needs: checks
11 | runs-on: ubuntu-latest
12 | steps:
13 | - uses: actions/checkout@v4
14 | with:
15 | fetch-depth: 0
16 | - uses: actions/setup-node@v4
17 | with:
18 | node-version: 20
19 | - name: git config
20 | run: |
21 | git config user.name "${GITHUB_ACTOR}"
22 | git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
23 | - name: Init npm config
24 | run: npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN
25 | env:
26 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
27 | - run: npm install
28 | - run: npm run release -- --ci
29 | env:
30 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
31 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
33 |
--------------------------------------------------------------------------------
/.github/workflows/stale.yml:
--------------------------------------------------------------------------------
1 | name: 'Close stale issues and PRs'
2 | on:
3 | schedule:
4 | - cron: '30 0 * * *'
5 |
6 | jobs:
7 | stale:
8 | runs-on: ubuntu-latest
9 | steps:
10 | - uses: actions/stale@v9
11 | with:
12 | stale-issue-message: 'This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue'
13 | stale-pr-message: 'This pull request has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still intend to submit this pull request'
14 | close-issue-message: 'This issue has been automatically closed because it has been inactive for more than 4 weeks. Please reopen if you still need help on this issue'
15 | close-pr-message: 'This pull request has been automatically closed because it has been inactive for more than 4 weeks. Please reopen if you still intend to submit this pull request'
16 | days-before-stale: 21
17 | days-before-close: 5
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | coverage
3 | .DS_STORE
4 | .nyc_output
5 | .idea
6 | .vscode/
7 | *.sublime-project
8 | *.sublime-workspace
9 | *.log
10 | build
11 | dist
12 | yarn.lock
13 | shrinkwrap.yaml
14 | test/__app
15 | package-lock.json
16 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | package-lock=false
2 |
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | build
2 | docs
3 | *.html
4 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | # The MIT License
2 |
3 | Copyright 2021 Harminder Virk, contributors
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # @poppinss/utils
2 |
3 | > A toolkit of utilities used across all the AdonisJS, Edge, and Japa packages
4 |
5 | [![gh-workflow-image]][gh-workflow-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
6 |
7 | ## Why this package exists?
8 |
9 | Many of my open source projects (including AdonisJS) use many single-purpose utility packages from npm. Over the years, I have faced the following challenges when using these packages.
10 |
11 | - It takes a lot of time to find a perfect package for the use case. The package should be well maintained, have good test coverage, and not accumulate debt by supporting some old versions of Node.js.
12 | - Some packages are great, but they end up pulling a lot of unnecessary dependencies like [(requiring TypeScript as a prod dependency)](https://github.com/blakeembrey/change-case/issues/281)
13 | - Sometimes I end up using different packages for the same utility (because, I cannot remember what I used last time in that other package). So I want to spend time once choosing the one I need and then bundle it inside `@poppinss/utils`.
14 | - Some authors introduce breaking changes too often (not a criticism). Therefore, I prefer wrapping their packages with my external API only to absorb breaking changes in one place.
15 | - Rest are some handwritten utilities to fit my needs
16 |
17 | > **Note**: If you are creating an AdonisJS package, I highly recommend using this package since it is already part of the user's project dependencies.
18 |
19 | > **Warning**: This package is not for general use (outside the AdonisJS ecosystem). I will not add new helpers or remove any to cater to a broader audience.
20 |
21 | ## Other packages to use
22 |
23 | A note to self and others to consider the following packages.
24 |
25 | | Package | Description |
26 | | ------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
27 | | [he](https://www.npmjs.com/package/he) | For escaping HTML entities and encoding unicode symbols. Has zero dependencies |
28 | | [@sindresorhus/is](https://www.npmjs.com/package/@sindresorhus/is) | For advanced type checking. Has zero dependencies |
29 |
30 | ## Package size
31 |
32 | Even though I do not care much about the package size (most of work is consumed on server side), I am mindful around the utilities and ensure not end up using really big packages for smaller use-cases.
33 |
34 | Here's the last checked install size of this package.
35 |
36 |
37 |
38 |
39 |
40 | ## Installation
41 |
42 | Install the package from the npm registry as follows:
43 |
44 | ```sh
45 | npm i @poppinss/utils
46 |
47 | # Yarn lovers
48 | yarn add @poppinss/utils
49 | ```
50 |
51 | ## Exported modules
52 |
53 | Following are the exported modules. Only the generic helpers are shipped from the main path. The rest of the helpers are grouped inside sub-modules.
54 |
55 | ```ts
56 | // string sub-module
57 | import string from '@poppinss/utils/string'
58 |
59 | // string builder
60 | import string from '@poppinss/utils/string_builder'
61 |
62 | // json sub-module
63 | import json from '@poppinss/utils/json'
64 |
65 | // lodash sub-module
66 | import lodash from '@poppinss/utils/lodash'
67 |
68 | // assert sub-module
69 | import assert from '@poppinss/utils/assert'
70 |
71 | // main module
72 | import { base64, fsReadAll } from '@poppinss/utils'
73 |
74 | // types sub-module
75 | import { ReadAllFilesOptions } from '@poppinss/utils/types'
76 | ```
77 |
78 | ### String helpers
79 |
80 | A collection of helpers to perform operations on/related to a string value.
81 |
82 | ```ts
83 | import string from '@poppinss/utils/string'
84 | ```
85 |
86 | #### excerpt
87 |
88 | Generate an excerpt from a string value. If the input value contains HTML tags, we will remove them from the excerpt.
89 |
90 | ```ts
91 | const html = `
AdonisJS is a Node.js framework, and hence it requires Node.js to be installed on your computer. To be precise, we need at least the latest release of Node.js v14
.