@stone-js/config
Advanced tools
Comparing version 0.0.33 to 0.0.34
@@ -19,6 +19,2 @@ /** | ||
/** | ||
* Represents a ConfigItems map where properties are strings mapped to values of type T. | ||
*/ | ||
type ConfigItems<T = any> = Record<PropertyKey, T>; | ||
/** | ||
* Class representing a Config. | ||
@@ -28,5 +24,5 @@ * | ||
* | ||
* @template T | ||
* @template TObject | ||
*/ | ||
declare class Config<T = any> extends Proxiable { | ||
declare class Config<TObject extends object = Record<PropertyKey, unknown>> extends Proxiable { | ||
private items; | ||
@@ -39,3 +35,3 @@ /** | ||
*/ | ||
static create<T>(items?: ConfigItems<T>): Config<T>; | ||
static create<TObject extends object = Record<PropertyKey, unknown>>(items?: TObject): Config<TObject>; | ||
/** | ||
@@ -46,3 +42,3 @@ * Create a Config. | ||
*/ | ||
protected constructor(items?: ConfigItems<T>); | ||
protected constructor(items?: any); | ||
/** | ||
@@ -52,6 +48,13 @@ * Get the specified configuration value. | ||
* @param key - The key or keys to retrieve from the configuration. | ||
* @returns The configuration value. | ||
*/ | ||
get<TReturn = unknown>(key: PropertyKey): TReturn | undefined; | ||
/** | ||
* Get the specified configuration value. | ||
* | ||
* @param key - The key or keys to retrieve from the configuration. | ||
* @param fallback - The fallback value if the key does not exist. | ||
* @returns The configuration value. | ||
*/ | ||
get<R>(key: PropertyKey, fallback?: R): R; | ||
get<TReturn = unknown>(key: PropertyKey, fallback: TReturn): TReturn; | ||
/** | ||
@@ -61,6 +64,13 @@ * Get the first match configuration value. | ||
* @param keys - An array of keys to check. | ||
* @returns The first matching configuration value. | ||
*/ | ||
firstMatch<TReturn = unknown>(keys: PropertyKey[]): TReturn | undefined; | ||
/** | ||
* Get the first match configuration value. | ||
* | ||
* @param keys - An array of keys to check. | ||
* @param fallback - The fallback value if no key matches. | ||
* @returns The first matching configuration value. | ||
*/ | ||
firstMatch<R>(keys: PropertyKey[], fallback?: R): R; | ||
firstMatch<TReturn = unknown>(keys: PropertyKey[], fallback: TReturn): TReturn; | ||
/** | ||
@@ -72,3 +82,3 @@ * Get many configuration values. | ||
*/ | ||
getMany<R>(keys: PropertyKey[] | Record<PropertyKey, T>): R; | ||
getMany<TReturn = Record<PropertyKey, unknown>>(keys: PropertyKey[] | Record<PropertyKey, unknown>): TReturn; | ||
/** | ||
@@ -88,3 +98,3 @@ * Determine if the given configuration value exists. | ||
*/ | ||
set<V>(key: PropertyKey | PropertyKey[] | Record<PropertyKey, T>, value?: V): this; | ||
set<TValue>(key: PropertyKey | PropertyKey[] | Record<PropertyKey, unknown>, value?: TValue): this; | ||
/** | ||
@@ -97,3 +107,3 @@ * Allows providers to define the default config for a module. | ||
*/ | ||
add<V>(key: PropertyKey, value: V): this; | ||
add<TValue>(key: PropertyKey, value: TValue): this; | ||
/** | ||
@@ -104,3 +114,3 @@ * Get all of the configuration items as a literal object. | ||
*/ | ||
all(): ConfigItems<T>; | ||
all(): TObject; | ||
/** | ||
@@ -114,2 +124,2 @@ * Clear all of the configuration items. | ||
export { Config, type ConfigItems, Proxiable }; | ||
export { Config, Proxiable }; |
@@ -1,2 +0,3 @@ | ||
import { get, has, set, isObjectLike, mergeWith } from 'lodash-es'; | ||
import deepmerge from 'deepmerge'; | ||
import { get, has, set, isObjectLike } from 'lodash-es'; | ||
@@ -28,3 +29,3 @@ /** | ||
* | ||
* @template T | ||
* @template TObject | ||
*/ | ||
@@ -39,3 +40,3 @@ class Config extends Proxiable { | ||
*/ | ||
static create(items = {}) { | ||
static create(items) { | ||
return new this(items); | ||
@@ -48,3 +49,3 @@ } | ||
*/ | ||
constructor(items = {}) { | ||
constructor(items) { | ||
super({ | ||
@@ -90,4 +91,5 @@ get: (target, prop, receiver) => { | ||
getMany(keys) { | ||
const defaults = {}; | ||
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) }), {}); | ||
return entries.reduce((results, [key, fallback]) => ({ ...results, [key]: get(this.items, key, fallback) }), defaults); | ||
} | ||
@@ -111,3 +113,3 @@ /** | ||
set(key, value) { | ||
const entries = typeof key === 'object' ? Object.entries(key) : [[key, value]]; | ||
const entries = !Array.isArray(key) && typeof key === 'object' ? Object.entries(key) : [[key, value]]; | ||
for (const [name, val] of entries) { | ||
@@ -126,5 +128,9 @@ set(this.items, name, val); | ||
add(key, value) { | ||
if (this.has(key) && isObjectLike(this.get(key)) && isObjectLike(value)) { | ||
mergeWith(value, this.get(key)); | ||
const items = this.get(key); | ||
if (Array.isArray(items)) { | ||
return this.set(key, items.concat(value)); | ||
} | ||
else if (isObjectLike(items) && isObjectLike(value)) { | ||
return this.set(key, deepmerge(items, value)); | ||
} | ||
return this.set(key, value); | ||
@@ -131,0 +137,0 @@ } |
{ | ||
"name": "@stone-js/config", | ||
"version": "0.0.33", | ||
"description": "Fluent and simple API with deep dot access to manage configurations in JavaScript any project", | ||
"version": "0.0.34", | ||
"description": "Fluent and simple API with deep dot access to manage configurations in JavaScript/TypeScript any project", | ||
"author": "Mr. Stone <evensstone@gmail.com>", | ||
@@ -52,2 +52,3 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"deepmerge": "^4.3.1", | ||
"lodash-es": "^4.17.21" | ||
@@ -54,0 +55,0 @@ }, |
@@ -6,5 +6,5 @@ # Stone.js: Config | ||
[![npm](https://img.shields.io/npm/dm/@stone-js/config)](https://www.npmjs.com/package/@stone-js/config) | ||
![Maintenance](https://img.shields.io/maintenance/yes/2024) | ||
![Maintenance](https://img.shields.io/maintenance/yes/2025) | ||
[![Publish Package to npmjs](https://github.com/stonemjs/config/actions/workflows/release.yml/badge.svg)](https://github.com/stonemjs/config/actions/workflows/release.yml) | ||
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=stonemjs/config)](https://dependabot.com) | ||
[![Dependabot Status](https://img.shields.io/badge/Dependabot-enabled-brightgreen.svg?logo=dependabot)](https://github.com/stonemjs/config/network/updates) | ||
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) | ||
@@ -11,0 +11,0 @@ |
28092
260
2
+ Addeddeepmerge@^4.3.1
+ Addeddeepmerge@4.3.1(transitive)