# sparejs

> default value `typeof value === 'undefined'? 'defaultValue': value` syntactic sugar

Latest version **0.4.0** (published 2017-08-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install sparejs
pnpm add sparejs
yarn add sparejs
bun add sparejs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2017-08-07 |
| First published | 2017-06-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | NimoChu |
| Maintainers | nimojs |
| Keywords | defaults, default, value, typeof, undefined |

## Links

- npm: https://www.npmjs.com/package/sparejs
- Repository: https://github.com/fast-flow/spare
- Homepage: https://github.com/fast-flow/spare#readme
- Issues: https://github.com/fast-flow/spare/issues
- npm.io page: https://npm.io/package/sparejs

## Dependencies (1)

- [extend](https://npm.io/package/extend.md) ^3.0.1

## Recent versions

- 0.4.0 (latest) — 2017-08-07
- 0.3.0 — 2017-08-07
- 0.2.0 — 2017-08-07
- 0.1.3 — 2017-06-22
- 0.1.2 — 2017-06-21
- 0.1.1 — 2017-06-21
- 0.1.0 — 2017-06-21

## README

# spare

> spare wheel

---

set default value syntactic sugar & set defaultSettings

```js
spare(data, attr?, defaultValue)
spare.settings(defaultSettings, userSettings)
// spare.settings equal: extend(true, extend(true, {}, defaultSettings), userSettings)
```

## install

```shell
npm install sparejs
```

```html
<!-- Script tag -->
<script src="https://pkgzip.com/?sparejs" ></script> <script>var spare = window.pkgzip.sparejs</script>
```

## Example

```js
spare(user, 'nimo')
// equal
typeof user === 'undefined'? 'nimo': user

spare(undefined, 'some')
// "some"

spare('nimo', 'some')
// "nimo"

var list
spare(list, ['1'])
// ['1']
```

```js
var self = {
    state: {
        form: {
            nickname: 'Nico'
        }
    }
}
spare(self.state.form, 'user.name', 'defaultValue')
// "defaultValue"

spare(self.state.form, 'nickname', 'defaultValue')
// "Nico"
```


## set

### Object

```js
var defaultValue = {
    name: 'nimo',
    age: 24,
    child: {
        'xxx': {
            age: 0,
            name: 'xxx'
        }
    }
}
```

```js
spare.settings(
    defaultValue
    ,
    {
        name: 'tim'
    }
)
/* - result
{
    name: 'tim',
    age: 24,
    child: {
        'xxx': {
            age: 0,
            name: 'xxx'
        }
    }
}
*/
```

```js
spare.settings(
    defaultValue
    ,
    {
        child: {
            xxx: {
                age: 1
            }
        }
    }
)
/* - result
{
    name: 'nimo',
    age: 24,
    child: {
        'xxx': {
            age: 1,
            name: 'xxx'
        }
    }
}
*/
```

### Array

```js
spare.settings(
    [1],
    [2,3]
)
// [2,3]
```

```js
spare.settings(
    [1],
    []
)
// [1]
```

```js
spare.settings(
    [1, [2]],
    [undefined,[3]]
)
// [1,[3]]
```

```js
spare.settings(
    [1, [2,10]],
    [undefined,[3,4,5]]
)
// [1,[3,4,5]]
```

---
_Source: https://npm.io/package/sparejs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
