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

Make the raw functions available on instance.$storage and Vue.storage #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Default: `(k, v) => localStorage.set(k, v)`
The function we use to store data.

##### clear

Type: `function`<br>
Default: `k => localStorage.removeItem(k)`

Expand All @@ -92,6 +92,85 @@ Type: `number`<br>
Default: `options.expiration`


### this.$storage.write(key, value)

#### key

Type: `String`<br>
Required: `true`

The name of the key you want to create/update.

#### value

Type: `String`<br>
Required: `true`

The value you want to give the key you are creating/updating.


### this.$storage.writeMulti(items)

#### items

Type: `Array`<br>
Required: `true`

An array of key/value pairs to store.


### this.$storage.read(key)

#### key

Type: `String`<br>
Required: `true`

The name of the key you want to retrieve the value of.

#### Retuns

The value of the key. If the key does not exist, null is returned.


### this.$storage.readMulti(keys)

#### keys

Type: `Array`<br>
Required: `true`

An array of key names to get the values of.

#### Retuns

An array of values based on the given keys.


### this.$storage.clear(key)

#### key

Type: `String`<br>
Required: `true`

The name of the key you want to remove from storage.


### this.$storage.clearMulti(keys)

#### keys

Type: `Array`<br>
Required: `true`

An array of key names to remove from storage.



**Note: this.$storage is also available on the global Vue as Vue.storage**


## Contributing

1. Fork it!
Expand Down
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ export default function (Vue, {
}
}
}

Vue.storage = {
write,
writeMulti: items => items.forEach(data => write(data.key, data.value)),
read,
readMulti: keys => { return keys.map(key => read(key)) },
clear,
clearMulti: keys => keys.forEach(key => clear(key))
}

Object.defineProperties(Vue.prototype, {
$storage: {
get() {
return Vue.storage;
}
}
})
}

function getExpiration(exp) {
Expand Down