npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuex-intern

v0.1.0

Published

Easily build Vuex getters and mutations

Downloads

97

Readme

vuex-intern

vuex-intern is a set of functions that facilitate common types of mutations and getters in Vuex

Install

npm install vuex-intern

Docs

Here is the state for all examples.

const state = {
	users: [
		{ id: 1, status: 'ACTIVE' },
		{ id: 2, status: 'ACTIVE' },
		{ id: 3, status: 'INACTIVE' }
	],
	userIndex: 0,
	open: false,
	authorizedUser: null,
	tags: ['book', 'movie', 'walking tour']
}

Getters

findByKey

findByKey finds an object within a list by searching for a key/value pair. If passed a single value, it will return a single object (if found). If passed an array, it will return an array. The first argument ('users' in the example below) can be an array if the property is nested in state.

import { findByKey } from 'vuex-intern'

// configure
const getters = {
	userById: findByKey('users', 'id')
}

// use
const user = store.getters.userById(1)
const users = store.getters.userById([1, 2, 3])

filterByKey

filterByKey filters a list against a key/value pair. filterByKey accepts a single value or an array of potential values. The first argument ('users' in the example below) can be an array if the property is nested in state.

import { filterByKey } from 'vuex-intern'

// configure
const getters = {
	usersByStatus: filterByKey('users', 'status')
}

// use
const activeUsers = store.getters.usersByStatus('ACTIVE')
const inactiveUsers = store.getters.usersByStatus(['INACTIVE', 'SUSPENDED'])

Mutations

adjustListIndex

adjustListIndex will adjust an array index in state by the provided value. Use a positive value to move forward in the index or a negative value to move backwards. For the list, you can pass a key to a list in state ("users" in the example below), or you can pass an actual array.

import { adjustListIndex } from 'vuex-intern'

// configure
const mutations = {
	moveUserIndex: adjustListIndex('userIndex', 'users')
}

// use
store.commit('moveUserIndex', 1)
// userIndex is now 1
store.commit('moveUserIndex', -2)
// userIndex is now 2

assignConstant

assignConstant will merge the same object into state, regardless of the value passed to the commit call. It's useful for resetting parts of state to initial values.

import { assignConstant } from 'vuex-intern'

// configure
const mutations = {
	reset: assignConstant(initialState)
}

// use
store.commit('reset')

extendRecordInList

extendRecordInList will add a record to a list or merge the new values into the existing record.

import { extendRecordInList } from 'vuex-intern'

// configure
const mutations = {
	addUser: extendRecordInList('users', 'id')
}

// use
// add record
store.commit('addUser', { id: 4, status: 'ACTIVE' })
// extend record
store.commit('addUser', { id: 4, username: 'jez' })
// record is now { id: 4, status: 'ACTIVE', username: 'jez' }

replaceRecordInList

replaceRecordInList will add a record to a list or replace the record if it exists in the list.

import { replaceRecordInList } from 'vuex-intern'

// configure
const mutations = {
	addUser: replaceRecordInList('users', 'id')
}

// use
// add record
store.commit('addUser', { id: 4, status: 'ACTIVE' })
// extend record
store.commit('addUser', { id: 4, username: 'jez' })
// record is now { id: 4, username: 'jez' }

set

set is an abstraction over setProp and setPath.

import { set } from 'vuex-intern'

const mutations = {
	setAuthorizedUser: set('authorizedUser'), // uses setProp
	setAuthToken: set(['authorizedUser', 'token']) // uses setPath
}

// use
store.commit('setAuthorizedUser', { token: 'abc', uid: '1' })
store.commit('setAuthToken', 'cbd')

setPath

setPath sets a value at a path within state. It creates the objects and arrays within the path if they don't exist. An integer in the path will cause an array to be created. A string in the path will cause an object to be created, even if it's "0".

import { setPath } from 'vuex-intern'

const mutations = {
	setAuthToken: setPath(['authorizedUser', 'token'])
}

// use
store.commit('setAuthToken', 'cbd')

setProp

setProp sets a value in state

import { setProp } from 'vuex-intern'

const mutations = {
	setAuthorizedUser: setProp('authorizedUser')
}

// use
store.commit('setAuthorizedUser', { token: 'abc', uid: '1' })

toggle

toggle toggles a boolean value.

import { toggle } from 'vuex-intern'

const mutations = {
	toggleOpen: toggle('open')
}

// use
store.commit('toggleOpen')

without

without removes items from a list in state. without can remove simple items, like strings, or it can remove records, if configured with a "targetKey" like "id" below.

import { without } from 'vuex-intern'

const mutations = {
	removeUsers: without('users', 'id')
	removeTags: without('tags')
}

// use
store.commit('removeTags', ['movie', 'walking tour'])
// tags is now ['book']
store.commit('removeUsers', [1, 2])
// users is now [{ id: 3, status: 'INACTIVE' }]