Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #29

Merged
merged 3 commits into from Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions .editorconfig
Expand Up @@ -8,7 +8,3 @@ indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{**/{actual,fixtures,expected,templates}/**,*.md}]
trim_trailing_whitespace = false
insert_final_newline = false
44 changes: 19 additions & 25 deletions .eslintrc.json
Expand Up @@ -4,29 +4,17 @@
],

"env": {
"browser": false,
"es6": true,
"node": true,
"mocha": true
"es2021": true,
"node": true
},

"parserOptions":{
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true
}
},

"globals": {
"document": false,
"navigator": false,
"window": false
"parserOptions": {
"ecmaVersion": 12
},

"rules": {
"accessor-pairs": 2,
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, { "before": true, "after": true }],
"block-spacing": [2, "always"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
Expand All @@ -39,7 +27,7 @@
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"generator-star-spacing": [2, { "before": true, "after": true }],
"handle-callback-err": [2, "^(err|error)$" ],
"handle-callback-err": [2, "^(err|error)$"],
"indent": [2, 2, { "SwitchCase": 1 }],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": [2, { "before": true, "after": true }],
Expand Down Expand Up @@ -68,18 +56,20 @@
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-implicit-coercion": 2,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-lonely-if": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multi-spaces": 0,
"no-multi-str": 2,
"no-multiple-empty-lines": [2, { "max": 1 }],
"no-native-reassign": 0,
"no-native-reassign": 2,
"no-negated-in-lhs": 2,
"no-new": 2,
"no-new-func": 2,
Expand All @@ -89,7 +79,7 @@
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-proto": 0,
"no-proto": 2,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-return-assign": 2,
Expand All @@ -100,28 +90,32 @@
"no-sparse-arrays": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 0,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-unexpected-multiline": 2,
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
"no-unreachable": 2,
"no-unused-expressions": 2,
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
"no-useless-call": 0,
"no-useless-call": 2,
"no-with": 2,
"one-var": [0, { "initialized": "never" }],
"object-curly-spacing": ["error", "always", { "objectsInObjects": true }],
"one-var": [2, { "initialized": "never" }],
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [0, "never"],
"prefer-const": [2, { "destructuring": "all", "ignoreReadBeforeAssign": false }],
"quotes": [2, "single", "avoid-escape"],
"radix": 2,
"semi": [2, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "never"],
"space-before-function-paren": [2, { "anonymous": "never", "named": "never", "asyncArrow": "always" }],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
"strict": 2,
"use-isnan": 2,
"valid-typeof": 2,
"wrap-iife": [2, "any"],
Expand Down
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: [jonschlinkert, doowb]
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
@@ -0,0 +1,20 @@
name: Tests
on: [push, pull_request]

jobs:
test:
name: Node.js ${{ matrix.node-version }} @ ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [11, 12, 13, 14, 15, 16]

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

107 changes: 84 additions & 23 deletions .verb.md
Expand Up @@ -6,26 +6,31 @@

```js
const set = require('{%= name %}');
set(object, prop, value);

const obj = {};
set(obj, 'a.b.c', 'd');

console.log(obj);
//=> { a: { b: { c: 'd' } } }
```

### Params

- `object` **{object}**: The object to set `value` on
- `prop` **{string}**: The property to set. Dot-notation may be used.
- `value` **{any}**: The value to set on `object[prop]`
Signature:

```js
set(object, property_path, value[, options]);
```

## Examples
- `object` **{Object}**: The object to set `value` on
- `path` **{String|Symbol|Array}**: The [path](#object-paths) of the property to set.
- `value` **{any}**: The value to set on `obj[prop]`
- `options` **{Object}**: See all [available options](#options)

Updates and returns the given object:
### Object paths

You may pass a string, symbol, or array of strings or symbols. By default, when a string is passed this library will split the string on `.` or a [custom separator](#options-separator) It's useful to pass an array

```js
const obj = {};
set(obj, 'a.b.c', 'd');
console.log(obj);
//=> { a: { b: { c: 'd' } } }
```

### Escaping

Expand All @@ -41,11 +46,67 @@ console.log(set({}, 'a\\.b\\.c', 'd'));
//=> { 'a.b.c': 'd' }
```

## Benchmarks
## Options

### options.preservePaths

Do not split properties that include a `/`. By default, set-value assumes that properties with a `/` are not intended to be split. This option allows you to disable default behavior.

Note that this option cannot be used if `options.separator` is set to `/`.

**Type**: `boolean`

**Default**: `true`

**Example**

```js
console.log(set({}, 'https://github.com', true));
//=> { 'https://github.com': true }

console.log(set({}, 'https://github.com', true, { preservePaths: false }));
//=> { 'https://github': { com: true } }
```

### options.separator

Custom separator to use for splitting object paths.

**Type**: `string`

**Default**: `.`

**Example**

```js
console.log(set(obj, 'auth/userpass/users/bob', '*****', { separator: '/' }));
//=> { auth: { userpass: { users: { bob: '*****' } } } }
```

### options.split

_(benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3)_.
Custom `.split()` function to use.


### options.merge

Allows you to update plain object values, instead of overwriting them.

**Type**: `boolean|function` - A custom `merge` function may be defined if you need to deep merge. Otherwise, when `merge` is `true`, a shallow merge will be performed by `Object.assign()`.

**Default**: `undefined`

**Example**

```js
const obj = { foo: { bar: { baz: 'qux' } } };
set(obj, 'foo.bar.fez', 'zzz', { merge: true });
//=> { foo: { bar: { baz: 'qux', fez: 'zzz' } } }
```

## Benchmarks

set-value is more reliable and has more features than dot-prop, without sacrificing performance.
Benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3.

```
{%= include("./benchmark/stats.md") %}
Expand All @@ -67,9 +128,9 @@ $ npm install && node benchmark

## Comparisons to other libs, or _"the list of shame"_

These are just a few of the duplicate libraries on NPM.
These are just a few of the duplicate libraries on NPM.

- [bury][] fails all of the tests. I even wrapped it to have it return the object instead of the value, but with all of that work it still fails the vast majority of tests.
- [bury][] fails all of the tests. I even wrapped it to have it return the object instead of the value, but with all of that work it still fails the vast majority of tests.
- [deep-get-set][] fails 22 of 26 unit tests.
- [deep-object][] fails 25 of 26 unit tests, completely butchered given objects.
- [deep-property][] fails 17 of 26 unit tests.
Expand All @@ -89,23 +150,23 @@ These are just a few of the duplicate libraries on NPM.

**Others that do the same thing, but use a completely different API**

- [deep-set-in][]
- [set-deep][]
- [set-deep-prop][]
- [bury][]
- [deep-set-in][]
- [set-deep][]
- [set-deep-prop][]
- [bury][]
- Many dozens of others


## History

### v3.0.0

- Added support for a custom `split` function to be passed on the options.
- Added support for a custom `split` function to be passed on the options.
- Removed support for splitting on brackets, since a [custom function][split-string] can be passed to do this now.

### v2.0.0

- Adds support for escaping with double or single quotes. See [escaping](#escaping) for examples.
- Will no longer split inside brackets or braces. See [bracket support](#bracket-support) for examples.

If there are any regressions please create a [bug report](../../issues/new). Thanks!
If there are any regressions please create a [bug report](../../issues/new). Thanks!
2 changes: 0 additions & 2 deletions FUNDING.yml

This file was deleted.