@stone-js/config
Advanced tools
Comparing version 0.0.31 to 0.0.32
@@ -21,4 +21,3 @@ /** | ||
*/ | ||
type ConfigItems<T> = Record<string, T>; | ||
type ConfigItems<T = any> = Record<PropertyKey, T>; | ||
/** | ||
@@ -45,3 +44,3 @@ * Class representing a Config. | ||
*/ | ||
constructor(items?: ConfigItems<T>); | ||
protected constructor(items?: ConfigItems<T>); | ||
/** | ||
@@ -54,3 +53,3 @@ * Get the specified configuration value. | ||
*/ | ||
get<R>(key: string | string[] | ConfigItems<R>, fallback?: R | null): R | ConfigItems<R>; | ||
get<R>(key: PropertyKey, fallback?: R): R | undefined; | ||
/** | ||
@@ -63,3 +62,3 @@ * Get the first match configuration value. | ||
*/ | ||
firstMatch<R>(keys: string[], fallback?: R | null): R; | ||
firstMatch<R>(keys: PropertyKey[], fallback?: R): R; | ||
/** | ||
@@ -71,3 +70,3 @@ * Get many configuration values. | ||
*/ | ||
getMany<R>(keys: string[] | ConfigItems<R>): ConfigItems<R>; | ||
getMany<R>(keys: PropertyKey[] | Record<PropertyKey, T>): R; | ||
/** | ||
@@ -79,3 +78,3 @@ * Determine if the given configuration value exists. | ||
*/ | ||
has(key: string | string[]): boolean; | ||
has(key: PropertyKey | PropertyKey[]): boolean; | ||
/** | ||
@@ -88,3 +87,3 @@ * Set a given configuration value. | ||
*/ | ||
set<R>(key: string | string[] | ConfigItems<R>, value?: R | null): this; | ||
set<V>(key: PropertyKey | PropertyKey[] | Record<PropertyKey, T>, value?: V): this; | ||
/** | ||
@@ -97,3 +96,3 @@ * Allows providers to define the default config for a module. | ||
*/ | ||
defaults<R>(key: string | string[] | ConfigItems<R>, value: R | ConfigItems<R>): this; | ||
add<V>(key: PropertyKey, value: V): this; | ||
/** | ||
@@ -100,0 +99,0 @@ * Get all of the configuration items as a literal object. |
@@ -66,6 +66,3 @@ import { get, has, set, isObjectLike, mergeWith } from 'lodash-es'; | ||
*/ | ||
get(key, fallback = null) { | ||
if (!Array.isArray(key) && typeof key === 'object') { | ||
return this.getMany(key); | ||
} | ||
get(key, fallback) { | ||
return get(this.items, key, fallback); | ||
@@ -80,3 +77,3 @@ } | ||
*/ | ||
firstMatch(keys, fallback = null) { | ||
firstMatch(keys, fallback) { | ||
const firstKey = keys.find((v) => this.has(v)) ?? []; | ||
@@ -92,3 +89,3 @@ return get(this.items, firstKey, fallback); | ||
getMany(keys) { | ||
const entries = Array.isArray(keys) ? keys.map((v) => [v, null]) : Object.entries(keys); | ||
const entries = Array.isArray(keys) ? keys.map((v) => [v, undefined]) : Object.entries(keys); | ||
return entries.reduce((results, [key, fallback]) => ({ ...results, [key]: get(this.items, key, fallback) }), {}); | ||
@@ -112,3 +109,3 @@ } | ||
*/ | ||
set(key, value = null) { | ||
set(key, value) { | ||
const entries = typeof key === 'object' ? Object.entries(key) : [[key, value]]; | ||
@@ -127,3 +124,3 @@ for (const [name, val] of entries) { | ||
*/ | ||
defaults(key, value) { | ||
add(key, value) { | ||
if (this.has(key) && isObjectLike(this.get(key)) && isObjectLike(value)) { | ||
@@ -130,0 +127,0 @@ mergeWith(value, this.get(key)); |
{ | ||
"name": "@stone-js/config", | ||
"version": "0.0.31", | ||
"version": "0.0.32", | ||
"description": "Fluent and simple API with deep dot access to manage configurations in JavaScript any project", | ||
"author": "Mr. Stone <evensstone@gmail.com>", | ||
"license": "Apache-2.0", | ||
"repository": "git@github.com:stonemjs/config.git", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/stonemjs/config.git" | ||
}, | ||
"homepage": "https://github.com/stonemjs/config#readme", | ||
@@ -9,0 +12,0 @@ "bugs": { |
@@ -20,6 +20,4 @@ # Stone.js: Config | ||
To install the `Config` utility, you need to add it to your project. Assuming it’s part of a package you manage. | ||
The `Config` library is available from the [`npm registry`](https://www.npmjs.com/) and can be installed as follows: | ||
NPM: | ||
```bash | ||
@@ -41,6 +39,12 @@ npm i @stone-js/config | ||
> [!NOTE] | ||
> This package is Pure ESM. If you are unfamiliar with what that means or how to handle it in your project, | ||
> please refer to [`this guide on Pure ESM packages`](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). | ||
Make sure your project setup is compatible with ESM. This might involve updating your `package.json` or using certain bundler configurations. | ||
The `Config` module can only be imported via ESM import syntax: | ||
```typescript | ||
import * as Config from '@stone-js/config'; | ||
import { Config } from '@stone-js/config'; | ||
``` | ||
@@ -113,10 +117,26 @@ | ||
### Managing Default Values | ||
The `defaults` method allows you to set default values for keys that do not already exist: | ||
### Adding Configuration Values with Merge | ||
The `add` method allows you to add configuration values. If the key already exists and both values are objects, they will be merged: | ||
```typescript | ||
config.defaults('settings.fontSize', 'medium'); | ||
console.log(config.get('settings.fontSize')); // Outputs: 'medium' | ||
config.add('settings', { notifications: false }); | ||
console.log(config.get('settings.notifications')); // Outputs: false | ||
``` | ||
### Getting the First Match Configuration Value | ||
The `firstMatch` method allows you to get the value of the first existing key from an array of keys: | ||
```typescript | ||
const value = config.firstMatch(['nonExistentKey', 'settings.theme'], 'defaultTheme'); | ||
console.log(value); // Outputs: 'light' | ||
``` | ||
### Getting Multiple Configuration Values | ||
The `getMany` method allows you to get multiple configuration values at once: | ||
```typescript | ||
const values = config.getMany(['appName', 'settings.theme']); | ||
console.log(values); // Outputs: { appName: 'MyApp', 'settings.theme': 'light' } | ||
``` | ||
### Clearing Configuration | ||
@@ -139,3 +159,3 @@ You can clear all configuration values using the `clear` method: | ||
### Summary | ||
## Summary | ||
The `@stone-js/config` library is a powerful and flexible solution for managing configuration in JavaScript and TypeScript applications. With support for nested properties, default value handling, and proxy-based custom behaviors, it provides a robust toolset for configuration management. | ||
@@ -142,0 +162,0 @@ |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
27152
177
0
244