Socket
Socket
Sign inDemoInstall

@poppinss/utils

Package Overview
Dependencies
Maintainers
1
Versions
89
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@poppinss/utils - npm Package Compare versions

Comparing version 2.5.3 to 2.5.4

build/src/defineStaticProperty.d.ts

1

build/index.d.ts

@@ -16,2 +16,3 @@ export { base64 } from './src/base64';

export { MessageBuilder } from './src/MessageBuilder';
export { defineStaticProperty } from './src/defineStaticProperty';
export { ManagerConfigValidator } from './src/ManagerConfigValidator';

@@ -60,3 +60,5 @@ "use strict";

Object.defineProperty(exports, "MessageBuilder", { enumerable: true, get: function () { return MessageBuilder_1.MessageBuilder; } });
var defineStaticProperty_1 = require("./src/defineStaticProperty");
Object.defineProperty(exports, "defineStaticProperty", { enumerable: true, get: function () { return defineStaticProperty_1.defineStaticProperty; } });
var ManagerConfigValidator_1 = require("./src/ManagerConfigValidator");
Object.defineProperty(exports, "ManagerConfigValidator", { enumerable: true, get: function () { return ManagerConfigValidator_1.ManagerConfigValidator; } });

3

package.json
{
"name": "@poppinss/utils",
"version": "2.5.3",
"version": "2.5.4",
"description": "Handy utilities for repetitive work",

@@ -83,2 +83,3 @@ "main": "build/index.js",

"fs-readdir-recursive": "^1.1.0",
"klona": "^2.0.3",
"ms": "^2.1.2",

@@ -85,0 +86,0 @@ "require-all": "^3.0.0",

<div align="center"><img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1557762307/poppinss_iftxlt.jpg" width="600px"></div>
# Utils
> Collection of reusable scripts used by AdonisJS core team

@@ -35,2 +36,3 @@

- [Message Builder](#message-builder)
- [defineStaticProperty](#definestaticproperty)

@@ -58,2 +60,3 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Exception
A custom exception class that extends the `Error` class to add support for defining `status` and `error codes`.

@@ -69,2 +72,3 @@

## fsReadAll
A utility to recursively read all script files for a given directory. This method is equivalent to

@@ -88,2 +92,3 @@ `readdir + recursive + filter (.js, .json, .ts)`.

## requireAll
Same as `fsReadAll`, but instead require the files. Helpful when you want to load all the config files inside a directory on app boot.

@@ -102,2 +107,3 @@

## esmRequire
Utility to require script files wihtout worrying about `CommonJs` and `ESM` exports. This is how it works.

@@ -110,5 +116,6 @@

**foo.js**
```ts
module.exports = {
greeting: 'Hello world'
greeting: 'Hello world',
}

@@ -118,5 +125,6 @@ ```

**foo.default.js**
```ts
export default {
greeting: 'Hello world'
greeting: 'Hello world',
}

@@ -126,5 +134,6 @@ ```

**foo.esm.js**
```ts
export const greeting = {
greeting: 'hello world'
greeting: 'hello world',
}

@@ -142,2 +151,3 @@ ```

## esmResolver
The `esmResolver` method works similar to `esmRequire`. However, instead of requiring the file, it accepts the object and returns the exported as per the same logic defined above.

@@ -162,2 +172,3 @@

## resolveFrom
Works similar to `require.resolve`, however it handles the absolute paths properly.

@@ -174,2 +185,3 @@

## resolveDir
The `require.resolve` or `resolveFrom` method can only resolve paths to a given file and not the directory. For example: If you pass path to a directory, then it will search for `index.js` inside it and in case of a package, it will be search for `main` entry point.

@@ -181,5 +193,3 @@

- Relative paths starting with `./` or `.\` are resolved using `path.join`.
- Path to packages inside `node_modules` are resolved as follows:
- Uses `require.resolve` to resolve the `package.json` file.
- Then replace the `package-name` with the absolute resolved package path.
- Path to packages inside `node_modules` are resolved as follows: - Uses `require.resolve` to resolve the `package.json` file. - Then replace the `package-name` with the absolute resolved package path.

@@ -200,2 +210,3 @@ ```ts

## interpolate
A small utility function to interpolate values inside a string.

@@ -213,2 +224,3 @@

## Lodash utilities
Lodash itself is a bulky library and most of the times, we don't need all the functions from it. For this purpose, the lodash team decided to publish individual methods to npm as packages. However, most of those individual packages are outdated.

@@ -230,2 +242,3 @@

### Exported methods
Following is the list of exported helpers.

@@ -245,2 +258,3 @@

## Base 64 Encode/Decode
Following helpers for base64 encoding/decoding also exists.

@@ -265,8 +279,11 @@

#### urlEncode
Same as `encode`, but safe for URLS and Filenames
#### urlDecode
Same as `decode`, but decodes the `urlEncode` output values
## Random String
A helper to generate random strings of a given length. Uses `crypto` under the hood.

@@ -281,2 +298,3 @@

## Safe equal
Compares two values by avoid [timing attack](https://en.wikipedia.org/wiki/Timing_attack). Accepts any input that can be passed to `Buffer.from`

@@ -291,2 +309,3 @@

## Safe stringify
Similar to `JSON.stringify`, but also handles Circular references by removing them.

@@ -308,2 +327,3 @@

## Safe parse
Similar to `JSON.parse`, but protects against [Prototype Poisoning](https://medium.com/intrinsic/javascript-prototype-poisoning-vulnerabilities-in-the-wild-7bc15347c96)

@@ -324,5 +344,6 @@

## Message Builder
Message builder provides a sane API for stringifying objects similar to `JSON.stringify` but has a few advantages.
- It is safe from JSON poisoning vulnerability.
- It is safe from JSON poisoning vulnerability.
- You can define expiry and purpose for the encoding. The `verify` method will respect these values.

@@ -335,7 +356,3 @@

const builder = new MessageBuilder()
const encoded = builder.build(
{ username: 'virk' },
'1 hour',
'login',
)
const encoded = builder.build({ username: 'virk' }, '1 hour', 'login')
```

@@ -351,15 +368,64 @@

## defineStaticProperty
Explicitly define static properties on a class by checking for `hasOwnProperty`. In case of inheritance, the properties from the parent class the cloned vs following the prototypal inheritance.
We use/need this copy from parent class behavior a lot in AdonisJS. Here's an example of Lucid models
You create an application wide base model
```ts
class AppModel extends BaseModel {
@column.datetime()
public createdAt: DateTime
}
```
AdonisJS will create the `$columnDefinitions` property on the `AppModel` class, that holds all the columns
```ts
AppModel.$columnDefinitions // { createdAt: { columName: created_at } }
```
Now, lets create another model inheriting the `AppModel`
```ts
class User extends AppModel {
@column()
public id: number
}
```
As per the Javascript prototypal inheritance. The `User` model will not contain the columns from the `AppModel`, because we just re-defined the `$columnDefinitions` property. However, we don't want this behavior and instead want to copy the columns from the `AppModel` and then add new columns to it.
Voila! Use the `defineStaticProperty` helper from this class.
```ts
class LucidBaseModel {
static boot() {
defineStaticProperty(this, LucidBaseModel, {
propertyName: '$columnDefinitions',
defaultValue: {},
strategy: 'inherit',
})
}
}
```
The `defineStaticProperty` takes a total of three arguments.
- The first argument is always `this`.
- The second argument is the root level base class. This will usually be the class exported by your package or module.
- The third argument takes the `propertyName`, `defaultValue (in case, there is nothing to copy)`, and the `strategy`.
- The `inherit` strategy will copy the properties from the base class.
- The `define` strategy will always use the `defaultValue` to define the property on the class. In other words, there is no copy behavior, but prototypal inheritance chain is also breaked by explicitly re-defining the property.
[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/utils/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/poppinss/utils "circleci"
[circleci-url]: https://circleci.com/gh/poppinss/utils 'circleci'
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
[typescript-url]: "typescript"
[npm-image]: https://img.shields.io/npm/v/@poppinss/utils.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@poppinss/utils "npm"
[npm-url]: https://npmjs.org/package/@poppinss/utils 'npm'
[license-image]: https://img.shields.io/npm/l/@poppinss/utils?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"
[license-url]: LICENSE.md 'license'
[audit-report-image]: https://img.shields.io/badge/-Audit%20Report-blueviolet?style=for-the-badge
[audit-report-url]: https://htmlpreview.github.io/?https://github.com/poppinss/utils/blob/develop/npm-audit.html "audit-report"
[audit-report-url]: https://htmlpreview.github.io/?https://github.com/poppinss/utils/blob/develop/npm-audit.html 'audit-report'
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc