forked from daren.hsu/line_push
update
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 - Pooya Parsa <pyapar@gmail.com>
|
||||
Copyright (c) UnJS
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
+12
-4
@@ -43,6 +43,12 @@ db.enabled=true
|
||||
update({ 'db.enabled': true }) // or update(..., { name: '.conf' })
|
||||
```
|
||||
|
||||
Push to an array:
|
||||
|
||||
```ts
|
||||
update({ 'modules[]': 'test' })
|
||||
```
|
||||
|
||||
**Read/Write config:**
|
||||
|
||||
```ts
|
||||
@@ -83,6 +89,8 @@ It means that you can use `.` for keys to define objects. Some examples:
|
||||
|
||||
**Note:** If you use keys that can override like `x=` and `x.y=`, you can disable this feature by passing `flat: true` option.
|
||||
|
||||
**Tip:** You can use keys ending with `[]` to push to an array like `test[]=A`
|
||||
|
||||
## Native Values
|
||||
|
||||
RC uses [destr](https://www.npmjs.com/package/destr) to convert values into native javascript values.
|
||||
@@ -140,8 +148,8 @@ MIT. Made with 💖
|
||||
[npm-downloads-src]: https://img.shields.io/npm/dm/rc9?style=flat-square
|
||||
[npm-downloads-href]: https://npmjs.com/package/rc9
|
||||
|
||||
[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/rc9/ci/master?style=flat-square
|
||||
[github-actions-href]: https://github.com/nuxt-contrib/rc9/actions?query=workflow%3Aci
|
||||
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/rc9/ci/main?style=flat-square
|
||||
[github-actions-href]: https://github.com/unjs/rc9/actions?query=workflow%3Aci
|
||||
|
||||
[codecov-src]: https://img.shields.io/codecov/c/gh/nuxt-contrib/rc9/master?style=flat-square
|
||||
[codecov-href]: https://codecov.io/gh/nuxt-contrib/rc9
|
||||
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/rc9/main?style=flat-square
|
||||
[codecov-href]: https://codecov.io/gh/unjs/rc9
|
||||
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const destr = require('destr');
|
||||
const flat = require('flat');
|
||||
const defu = require('defu');
|
||||
|
||||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
||||
|
||||
const destr__default = /*#__PURE__*/_interopDefaultLegacy(destr);
|
||||
const flat__default = /*#__PURE__*/_interopDefaultLegacy(flat);
|
||||
const defu__default = /*#__PURE__*/_interopDefaultLegacy(defu);
|
||||
|
||||
const RE_KEY_VAL = /^\s*([^=\s]+)\s*=\s*(.*)?\s*$/;
|
||||
const RE_LINES = /\n|\r|\r\n/;
|
||||
const defaults = {
|
||||
name: ".conf",
|
||||
dir: process.cwd(),
|
||||
flat: false
|
||||
};
|
||||
function withDefaults(options) {
|
||||
if (typeof options === "string") {
|
||||
options = { name: options };
|
||||
}
|
||||
return { ...defaults, ...options };
|
||||
}
|
||||
function parse(contents, options = {}) {
|
||||
const config = {};
|
||||
const lines = contents.split(RE_LINES);
|
||||
for (const line of lines) {
|
||||
const match = line.match(RE_KEY_VAL);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const key = match[1];
|
||||
if (!key || key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const val = destr__default(match[2].trim());
|
||||
if (key.endsWith("[]")) {
|
||||
const nkey = key.substr(0, key.length - 2);
|
||||
config[nkey] = (config[nkey] || []).concat(val);
|
||||
continue;
|
||||
}
|
||||
config[key] = val;
|
||||
}
|
||||
return options.flat ? config : flat__default.unflatten(config, { overwrite: true });
|
||||
}
|
||||
function parseFile(path, options) {
|
||||
if (!fs.existsSync(path)) {
|
||||
return {};
|
||||
}
|
||||
return parse(fs.readFileSync(path, "utf-8"), options);
|
||||
}
|
||||
function read(options) {
|
||||
options = withDefaults(options);
|
||||
return parseFile(path.resolve(options.dir, options.name), options);
|
||||
}
|
||||
function readUser(options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || os.homedir();
|
||||
return read(options);
|
||||
}
|
||||
function serialize(config) {
|
||||
return Object.entries(flat__default.flatten(config)).map(([key, val]) => `${key}=${typeof val === "string" ? val : JSON.stringify(val)}`).join("\n");
|
||||
}
|
||||
function write(config, options) {
|
||||
options = withDefaults(options);
|
||||
fs.writeFileSync(path.resolve(options.dir, options.name), serialize(config), {
|
||||
encoding: "utf-8"
|
||||
});
|
||||
}
|
||||
function writeUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || os.homedir();
|
||||
write(config, options);
|
||||
}
|
||||
function update(config, options) {
|
||||
options = withDefaults(options);
|
||||
if (!options.flat) {
|
||||
config = flat__default.unflatten(config, { overwrite: true });
|
||||
}
|
||||
const newConfig = defu__default(config, read(options));
|
||||
write(newConfig, options);
|
||||
return newConfig;
|
||||
}
|
||||
function updateUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || os.homedir();
|
||||
return update(config, options);
|
||||
}
|
||||
|
||||
exports.defaults = defaults;
|
||||
exports.parse = parse;
|
||||
exports.parseFile = parseFile;
|
||||
exports.read = read;
|
||||
exports.readUser = readUser;
|
||||
exports.serialize = serialize;
|
||||
exports.update = update;
|
||||
exports.updateUser = updateUser;
|
||||
exports.write = write;
|
||||
exports.writeUser = writeUser;
|
||||
+18
-17
@@ -1,17 +1,18 @@
|
||||
declare type RC = Record<string, any>;
|
||||
interface RCOptions {
|
||||
name?: string;
|
||||
dir?: string;
|
||||
flat?: boolean;
|
||||
}
|
||||
export declare const defaults: RCOptions;
|
||||
export declare function parse(contents: string, options?: RCOptions): RC;
|
||||
export declare function parseFile(path: string, options?: RCOptions): RC;
|
||||
export declare function read(options?: RCOptions | string): RC;
|
||||
export declare function readUser(options?: RCOptions | string): RC;
|
||||
export declare function serialize(config: RC): string;
|
||||
export declare function write(config: RC, options?: RCOptions | string): void;
|
||||
export declare function writeUser(config: RC, options?: RCOptions | string): void;
|
||||
export declare function update(config: RC, options?: RCOptions | string): RC;
|
||||
export declare function updateUser(config: RC, options?: RCOptions | string): RC;
|
||||
export {};
|
||||
declare type RC = Record<string, any>;
|
||||
interface RCOptions {
|
||||
name?: string;
|
||||
dir?: string;
|
||||
flat?: boolean;
|
||||
}
|
||||
declare const defaults: RCOptions;
|
||||
declare function parse(contents: string, options?: RCOptions): RC;
|
||||
declare function parseFile(path: string, options?: RCOptions): RC;
|
||||
declare function read(options?: RCOptions | string): RC;
|
||||
declare function readUser(options?: RCOptions | string): RC;
|
||||
declare function serialize(config: RC): string;
|
||||
declare function write(config: RC, options?: RCOptions | string): void;
|
||||
declare function writeUser(config: RC, options?: RCOptions | string): void;
|
||||
declare function update(config: RC, options?: RCOptions | string): RC;
|
||||
declare function updateUser(config: RC, options?: RCOptions | string): RC;
|
||||
|
||||
export { defaults, parse, parseFile, read, readUser, serialize, update, updateUser, write, writeUser };
|
||||
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import destr from 'destr';
|
||||
import flat from 'flat';
|
||||
import defu from 'defu';
|
||||
|
||||
const RE_KEY_VAL = /^\s*([^=\s]+)\s*=\s*(.*)?\s*$/;
|
||||
const RE_LINES = /\n|\r|\r\n/;
|
||||
const defaults = {
|
||||
name: ".conf",
|
||||
dir: process.cwd(),
|
||||
flat: false
|
||||
};
|
||||
function withDefaults(options) {
|
||||
if (typeof options === "string") {
|
||||
options = { name: options };
|
||||
}
|
||||
return { ...defaults, ...options };
|
||||
}
|
||||
function parse(contents, options = {}) {
|
||||
const config = {};
|
||||
const lines = contents.split(RE_LINES);
|
||||
for (const line of lines) {
|
||||
const match = line.match(RE_KEY_VAL);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const key = match[1];
|
||||
if (!key || key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const val = destr(match[2].trim());
|
||||
if (key.endsWith("[]")) {
|
||||
const nkey = key.substr(0, key.length - 2);
|
||||
config[nkey] = (config[nkey] || []).concat(val);
|
||||
continue;
|
||||
}
|
||||
config[key] = val;
|
||||
}
|
||||
return options.flat ? config : flat.unflatten(config, { overwrite: true });
|
||||
}
|
||||
function parseFile(path, options) {
|
||||
if (!existsSync(path)) {
|
||||
return {};
|
||||
}
|
||||
return parse(readFileSync(path, "utf-8"), options);
|
||||
}
|
||||
function read(options) {
|
||||
options = withDefaults(options);
|
||||
return parseFile(resolve(options.dir, options.name), options);
|
||||
}
|
||||
function readUser(options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
return read(options);
|
||||
}
|
||||
function serialize(config) {
|
||||
return Object.entries(flat.flatten(config)).map(([key, val]) => `${key}=${typeof val === "string" ? val : JSON.stringify(val)}`).join("\n");
|
||||
}
|
||||
function write(config, options) {
|
||||
options = withDefaults(options);
|
||||
writeFileSync(resolve(options.dir, options.name), serialize(config), {
|
||||
encoding: "utf-8"
|
||||
});
|
||||
}
|
||||
function writeUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
write(config, options);
|
||||
}
|
||||
function update(config, options) {
|
||||
options = withDefaults(options);
|
||||
if (!options.flat) {
|
||||
config = flat.unflatten(config, { overwrite: true });
|
||||
}
|
||||
const newConfig = defu(config, read(options));
|
||||
write(newConfig, options);
|
||||
return newConfig;
|
||||
}
|
||||
function updateUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
return update(config, options);
|
||||
}
|
||||
|
||||
export { defaults, parse, parseFile, read, readUser, serialize, update, updateUser, write, writeUser };
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 - UnJS
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
+165
@@ -0,0 +1,165 @@
|
||||

|
||||
|
||||
# 🌊 defu
|
||||
|
||||
> Assign default properties, recursively. Lightweight and Fast!
|
||||
|
||||
[![Standard JS][standard-src]][standard-href]
|
||||
[![codecov][codecov-src]][codecov-href]
|
||||
[![npm version][npm-v-src]][npm-v-href]
|
||||
[![npm downloads][npm-dm-src]][npm-dm-href]
|
||||
[![package phobia][packagephobia-src]][packagephobia-href]
|
||||
[![bundle phobia][bundlephobia-src]][bundlephobia-href]
|
||||
|
||||
## Install
|
||||
|
||||
Install package:
|
||||
|
||||
```bash
|
||||
# yarn
|
||||
yarn add defu
|
||||
# npm
|
||||
npm install defu
|
||||
# pnpm
|
||||
pnpm install defu
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { defu } from 'defu'
|
||||
|
||||
const options = defu(object, ...defaults)
|
||||
```
|
||||
|
||||
Leftmost arguments have more priority when assigning defaults.
|
||||
|
||||
### Arguments
|
||||
|
||||
- **object (Object):** The destination object.
|
||||
- **source (Object):** The source object.
|
||||
|
||||
```js
|
||||
import { defu } from 'defu'
|
||||
|
||||
console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
|
||||
// => { a: { b: 2, c: 3 } }
|
||||
```
|
||||
|
||||
### Using with CommonJS
|
||||
|
||||
```js
|
||||
const { defu } = require('defu')
|
||||
```
|
||||
|
||||
## Custom Merger
|
||||
|
||||
Sometimes default merging strategy is not desirable. Using `createDefu` we can create a custom instance with different merging strategy.
|
||||
|
||||
This function accepts `obj` (source object), `key` and `value` (current value) and should return `true` if applied custom merging.
|
||||
|
||||
**Example:** Sum numbers instead of overriding
|
||||
|
||||
```js
|
||||
import { createDefu } from 'defu'
|
||||
|
||||
const ext = createDefu((obj, key, value) => {
|
||||
if (typeof obj[key] === 'number' && typeof value === 'number') {
|
||||
obj[key] += val
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }
|
||||
```
|
||||
|
||||
## Function Merger
|
||||
|
||||
Using `defuFn`, if user provided a function, it will be called with default value instead of merging.
|
||||
|
||||
I can be useful for default values manipulation.
|
||||
|
||||
**Example:** Filter some items from defaults (array) and add 20 to the count default value.
|
||||
|
||||
```js
|
||||
import { defuFn } from 'defu'
|
||||
|
||||
defuFn({
|
||||
ignore: (val) => val.filter(item => item !== 'dist'),
|
||||
count: (count) => count + 20
|
||||
}, {
|
||||
ignore: ['node_modules','dist'],
|
||||
count: 10
|
||||
})
|
||||
/*
|
||||
{
|
||||
ignore: ['node_modules'],
|
||||
count: 30
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
**Note:** if the default value is not defined, the function defined won't be called and kept as value.
|
||||
|
||||
## Array Function Merger
|
||||
|
||||
`defuArrayFn` is similar to `defuFn` but **only applies to array values defined in defaults**.
|
||||
|
||||
**Example:** Filter some items from defaults (array) and add 20 to the count default value.
|
||||
|
||||
```js
|
||||
import { defuArrayFn } from 'defu'
|
||||
|
||||
defuArrayFn({
|
||||
ignore(val) => val.filter(i => i !== 'dist'),
|
||||
count: () => 20
|
||||
}, {
|
||||
ignore: [
|
||||
'node_modules',
|
||||
'dist'
|
||||
],
|
||||
count: 10
|
||||
})
|
||||
/*
|
||||
{
|
||||
ignore: ['node_modules'],
|
||||
count: () => 20
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
**Note:** the function is called only if the value defined in defaults is an aray.
|
||||
|
||||
### Remarks
|
||||
|
||||
- `object` and `defaults` are not modified
|
||||
- Nullish values ([`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) and [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)) are skipped. Please use [defaults-deep](https://www.npmjs.com/package/defaults-deep) or [omit-deep](http://npmjs.com/package/omit-deep) or [lodash.defaultsdeep](https://www.npmjs.com/package/lodash.defaultsdeep) if you need to preserve or different behavior.
|
||||
- Assignment of `__proto__` and `constructor` keys will be skipped to prevent security issues with object pollution.
|
||||
- Will concat `array` values (if default property is defined)
|
||||
```js
|
||||
console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
|
||||
// => { array: ['a', 'b', 'c']}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT. Made with 💖
|
||||
|
||||
<!-- Refs -->
|
||||
[standard-src]: https://flat.badgen.net/badge/code%20style/standard/green
|
||||
[standard-href]: https://standardjs.com
|
||||
|
||||
[npm-v-src]: https://flat.badgen.net/npm/v/defu/latest
|
||||
[npm-v-href]: https://npmjs.com/package/defu
|
||||
|
||||
[npm-dm-src]: https://flat.badgen.net/npm/dm/defu
|
||||
[npm-dm-href]: https://npmjs.com/package/defu
|
||||
|
||||
[packagephobia-src]: https://flat.badgen.net/packagephobia/install/defu
|
||||
[packagephobia-href]: https://packagephobia.now.sh/result?p=defu
|
||||
|
||||
[bundlephobia-src]: https://flat.badgen.net/bundlephobia/min/defu
|
||||
[bundlephobia-href]: https://bundlephobia.com/result?p=defu
|
||||
|
||||
[codecov-src]: https://flat.badgen.net/codecov/c/github/unjs/defu/master
|
||||
[codecov-href]: https://codecov.io/gh/unjs/defu
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
function isObject(val) {
|
||||
return val !== null && typeof val === "object";
|
||||
}
|
||||
function _defu(baseObj, defaults, namespace = ".", merger) {
|
||||
if (!isObject(defaults)) {
|
||||
return _defu(baseObj, {}, namespace, merger);
|
||||
}
|
||||
const obj = Object.assign({}, defaults);
|
||||
for (const key in baseObj) {
|
||||
if (key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const val = baseObj[key];
|
||||
if (val === null || val === void 0) {
|
||||
continue;
|
||||
}
|
||||
if (merger && merger(obj, key, val, namespace)) {
|
||||
continue;
|
||||
}
|
||||
if (Array.isArray(val) && Array.isArray(obj[key])) {
|
||||
obj[key] = val.concat(obj[key]);
|
||||
} else if (isObject(val) && isObject(obj[key])) {
|
||||
obj[key] = _defu(val, obj[key], (namespace ? `${namespace}.` : "") + key.toString(), merger);
|
||||
} else {
|
||||
obj[key] = val;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
function createDefu(merger) {
|
||||
return (...args) => args.reduce((p, c) => _defu(p, c, "", merger), {});
|
||||
}
|
||||
const defu = createDefu();
|
||||
const defuFn = createDefu((obj, key, currentValue, _namespace) => {
|
||||
if (typeof obj[key] !== "undefined" && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
const defuArrayFn = createDefu((obj, key, currentValue, _namespace) => {
|
||||
if (Array.isArray(obj[key]) && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
exports.createDefu = createDefu;
|
||||
exports["default"] = defu;
|
||||
exports.defu = defu;
|
||||
exports.defuArrayFn = defuArrayFn;
|
||||
exports.defuFn = defuFn;
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
declare type Input = Record<string | number | symbol, any>;
|
||||
declare type Merger = <T extends Input, K extends keyof T>(obj: T, key: keyof T, value: T[K], namespace: string) => any;
|
||||
declare type nullish = null | undefined | void;
|
||||
declare type MergeObjects<Destination extends Input, Defaults extends Input> = Destination extends Defaults ? Destination : Omit<Destination, keyof Destination & keyof Defaults> & Omit<Defaults, keyof Destination & keyof Defaults> & {
|
||||
-readonly [Key in keyof Destination & keyof Defaults]: Destination[Key] extends nullish ? Defaults[Key] extends nullish ? nullish : Defaults[Key] : Defaults[Key] extends nullish ? Destination[Key] : Merge<Destination[Key], Defaults[Key]>;
|
||||
};
|
||||
declare type DefuFn = <Source extends Input, Defaults extends Input>(source: Source, ...defaults: Defaults[]) => MergeObjects<Source, Defaults>;
|
||||
interface Defu {
|
||||
<Source extends Input, Defaults extends Input>(source: Source, ...defaults: Defaults[]): MergeObjects<Source, Defaults>;
|
||||
fn: DefuFn;
|
||||
arrayFn: DefuFn;
|
||||
extend(merger?: Merger): DefuFn;
|
||||
}
|
||||
declare type MergeArrays<Destination, Source> = Destination extends Array<infer DestinationType> ? Source extends Array<infer SourceType> ? Array<DestinationType | SourceType> : Source | Array<DestinationType> : Source | Destination;
|
||||
declare type Merge<Destination extends Input, Defaults extends Input> = Destination extends nullish ? Defaults extends nullish ? nullish : Defaults : Defaults extends nullish ? Destination : Destination extends Array<any> ? Defaults extends Array<any> ? MergeArrays<Destination, Defaults> : Destination | Defaults : Destination extends Function ? Destination | Defaults : Destination extends RegExp ? Destination | Defaults : Destination extends Promise<any> ? Destination | Defaults : Defaults extends Function ? Destination | Defaults : Defaults extends RegExp ? Destination | Defaults : Defaults extends Promise<any> ? Destination | Defaults : Destination extends Input ? Defaults extends Input ? MergeObjects<Destination, Defaults> : Destination | Defaults : Destination | Defaults;
|
||||
|
||||
declare function createDefu(merger?: Merger): DefuFn;
|
||||
declare const defu: Defu;
|
||||
|
||||
declare const defuFn: DefuFn;
|
||||
declare const defuArrayFn: DefuFn;
|
||||
|
||||
export { createDefu, defu as default, defu, defuArrayFn, defuFn };
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
function isObject(val) {
|
||||
return val !== null && typeof val === "object";
|
||||
}
|
||||
function _defu(baseObj, defaults, namespace = ".", merger) {
|
||||
if (!isObject(defaults)) {
|
||||
return _defu(baseObj, {}, namespace, merger);
|
||||
}
|
||||
const obj = Object.assign({}, defaults);
|
||||
for (const key in baseObj) {
|
||||
if (key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const val = baseObj[key];
|
||||
if (val === null || val === void 0) {
|
||||
continue;
|
||||
}
|
||||
if (merger && merger(obj, key, val, namespace)) {
|
||||
continue;
|
||||
}
|
||||
if (Array.isArray(val) && Array.isArray(obj[key])) {
|
||||
obj[key] = val.concat(obj[key]);
|
||||
} else if (isObject(val) && isObject(obj[key])) {
|
||||
obj[key] = _defu(val, obj[key], (namespace ? `${namespace}.` : "") + key.toString(), merger);
|
||||
} else {
|
||||
obj[key] = val;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
function createDefu(merger) {
|
||||
return (...args) => args.reduce((p, c) => _defu(p, c, "", merger), {});
|
||||
}
|
||||
const defu = createDefu();
|
||||
const defuFn = createDefu((obj, key, currentValue, _namespace) => {
|
||||
if (typeof obj[key] !== "undefined" && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
const defuArrayFn = createDefu((obj, key, currentValue, _namespace) => {
|
||||
if (Array.isArray(obj[key]) && typeof currentValue === "function") {
|
||||
obj[key] = currentValue(obj[key]);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
export { createDefu, defu as default, defu, defuArrayFn, defuFn };
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"defu@6.0.0",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "defu@6.0.0",
|
||||
"_id": "defu@6.0.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-t2MZGLf1V2rV4VBZbWIaXKdX/mUcYW0n2znQZoADBkGGxYL8EWqCuCZBmJPJ/Yy9fofJkyuuSuo5GSwo0XdEgw==",
|
||||
"_location": "/rc9/defu",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "defu@6.0.0",
|
||||
"name": "defu",
|
||||
"escapedName": "defu",
|
||||
"rawSpec": "6.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "6.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/rc9"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/defu/-/defu-6.0.0.tgz",
|
||||
"_spec": "6.0.0",
|
||||
"_where": "/home/node/nuxt",
|
||||
"bugs": {
|
||||
"url": "https://github.com/unjs/defu/issues"
|
||||
},
|
||||
"description": "Recursively assign default properties. Lightweight and Fast!",
|
||||
"devDependencies": {
|
||||
"@nuxtjs/eslint-config-typescript": "latest",
|
||||
"@types/node": "latest",
|
||||
"c8": "^7.11.0",
|
||||
"eslint": "latest",
|
||||
"expect-type": "latest",
|
||||
"standard-version": "latest",
|
||||
"typescript": "latest",
|
||||
"unbuild": "latest",
|
||||
"vitest": "^0.7.7"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/defu.cjs",
|
||||
"import": "./dist/defu.mjs"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"homepage": "https://github.com/unjs/defu#readme",
|
||||
"license": "MIT",
|
||||
"main": "./dist/defu.cjs",
|
||||
"module": "./dist/defu.mjs",
|
||||
"name": "defu",
|
||||
"packageManager": "pnpm@6.32.3",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/unjs/defu.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "vitest",
|
||||
"lint": "eslint --ext .ts src",
|
||||
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
|
||||
"test": "pnpm lint && pnpm vitest"
|
||||
},
|
||||
"types": "./dist/defu.d.ts",
|
||||
"version": "6.0.0"
|
||||
}
|
||||
+34
-27
@@ -1,39 +1,39 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"rc9@1.0.0",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"rc9@1.2.2",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "rc9@1.0.0",
|
||||
"_id": "rc9@1.0.0",
|
||||
"_from": "rc9@1.2.2",
|
||||
"_id": "rc9@1.2.2",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-MVgjFCXkndOD08FBqnCgX92ND36NaGVEzBuImhZ5XVFZz2rF9RGfTiQ0yczxdNEoNbwIRC1guhQJVUMljsqvTg==",
|
||||
"_integrity": "sha512-zbe8+HR2X28eZepAwohuKkebbEsA67h0DO9I7g12QrHa2CQopR9gztOLPIPXXGTvcxeUjAN4wZ+b29t3m/u05g==",
|
||||
"_location": "/rc9",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "rc9@1.0.0",
|
||||
"raw": "rc9@1.2.2",
|
||||
"name": "rc9",
|
||||
"escapedName": "rc9",
|
||||
"rawSpec": "1.0.0",
|
||||
"rawSpec": "1.2.2",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.0.0"
|
||||
"fetchSpec": "1.2.2"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@nuxt/config",
|
||||
"/@nuxt/telemetry"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/rc9/-/rc9-1.0.0.tgz",
|
||||
"_spec": "1.0.0",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/rc9/-/rc9-1.2.2.tgz",
|
||||
"_spec": "1.2.2",
|
||||
"_where": "/home/node/nuxt",
|
||||
"bugs": {
|
||||
"url": "https://github.com/nuxt-contrib/rc9/issues"
|
||||
"url": "https://github.com/unjs/rc9/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"defu": "^2.0.4",
|
||||
"destr": "^1.0.0",
|
||||
"defu": "^6.0.0",
|
||||
"destr": "^1.1.1",
|
||||
"flat": "^5.0.0"
|
||||
},
|
||||
"description": "Read/Write config couldn't be easier!",
|
||||
@@ -42,32 +42,39 @@
|
||||
"@types/flat": "latest",
|
||||
"@types/jest": "latest",
|
||||
"@types/node": "latest",
|
||||
"bili": "latest",
|
||||
"c8": "^7.11.0",
|
||||
"eslint": "latest",
|
||||
"jest": "latest",
|
||||
"rollup-plugin-typescript2": "latest",
|
||||
"standard-version": "latest",
|
||||
"ts-jest": "latest",
|
||||
"typescript": "latest"
|
||||
"typescript": "latest",
|
||||
"unbuild": "^0.7.2",
|
||||
"vitest": "^0.9.0"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./dist/index.mjs"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"homepage": "https://github.com/nuxt-contrib/rc9#readme",
|
||||
"homepage": "https://github.com/unjs/rc9#readme",
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"main": "./dist/index.cjs",
|
||||
"name": "rc9",
|
||||
"packageManager": "pnpm@6.32.3",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/nuxt-contrib/rc9.git"
|
||||
"url": "git+https://github.com/unjs/rc9.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bili src/index.ts --minimal",
|
||||
"build": "unbuild",
|
||||
"dev": "vitest",
|
||||
"lint": "eslint --ext .ts .",
|
||||
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
|
||||
"test": "yarn lint && jest"
|
||||
"release": "pnpm test && pnpm build && standard-version && git push --follow-tags && pnpm publish",
|
||||
"test": "pnpm lint && vitest run"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"types": "dist/index.d.ts",
|
||||
"version": "1.0.0"
|
||||
"types": "./dist/index.d.ts",
|
||||
"version": "1.2.2"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user