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

vutils

v0.15.3

Published

Utilities For VueJS

Downloads

69

Readme

vutils

Utilities for VueJS

How To Use

yarn add vutils  # or npm install vutils --save
import Vue from 'vue';
import VueUtils from 'vutils';

Vue.use(VueUtils);
// for using helper functions
import {remove, update, addOrUpdate} from 'vutils';

function saveUser(state, user) {
	addOrUpdate(state.users, user, 'id');
}

function deleteUser(state, user) {
	remove(state.users, item => item.id === user.id);
}

Extra Options

vModel

This will help you to define components which use v-model to sync data with parent component. By default it'll define a property named currentValue on the component, which you can modify and it'll automatically emit input and change events, so that data can be synced with parent component.

Note that if you're using vModel, you need to define value as a prop of the component.

export default {
	vModel: true,
	props: {
		value: {},
	},
	methods: {
		someMethod() {
			this.currentValue = 'updated value';
		}
	}
};
// using a custom name instead of currentValue
export default {
	vModel: 'shown',
	props: {
		value: {
			type: Boolean,
			default: true,
		}
	},
	methods: {
		someMethod() {
			this.shown = false;
		}
	}
};

Prop Modify

Since Vue doesn't allow us to modify the props directly, this can be used to define data which will keep in sync with the props and can be modified. You need to add modify: true to the props you wish to modify.

export default {
	props: {
		shown: {
			type: Boolean,
			modify: true,
		},
		formData: {
			type: Object,
			modify: 'user',
		},
	},

	methods: {
		someMethod() {
			this.iShown = false;
			this.user = {
				name: 'hello'
			};
		},
	},
};

By default it'll define a data property with the name i{CapitalizedPropName}. You can override that by giving a string instead of true in the modify parameter.

NOTE: It automatically clones the property whenever it is modified, so you don't need to worry about changing the original (parent) property in case the property is an object.

reEvents

This will re-emit the same event with a different name.

export default {
	reEvents: {save: 'done'},
	methods: {
		someMethod() {
			// this will emit both save and done event
			this.$emit('save');
		},
	},
};

Event Plugin

You can define global event handling in events.
Emit global events using this.$$emit.
You can also listen for global events using this.$$on(eventName, () => {})

export default {
	events: {
		someGlobalEvent(e) {
			// do something
		},
	},
};

// In some other component
export default {
	methods: {
		someMethod(e) {
			this.$$emit('someGlobalEvent');
		},
	},
};

Pagination Mixin

Pagination mixin helps in doing pagination and filtering.

It defines some methods in the component.
handleFilterChange: call this whenever a filter is changed
handleSizeChange: call this when items per page has changed
handleCurrentChange: call this when the current page has changed
handleSortChange: call this when sorting parameter changes
reloadSelfData: call this to reload the data in the page

And some data variables:
defaultSort: indicates the default sorting parameter of the component
loadingSelfData: indicates if the data of the component is loading or not.

You need to define a method called loadSelfData(filters) in your component methods which loads the data of the component. Ideally this method should return a promise.

You also need to define a data variable named filters, which should be an object and it denotes the filters that the component supports and their default values.

<template>
	<ela-content-layout padding="0">
		<div slot="head">
			<h3>Brands</h3>
		</div>

		<div slot="filters">
			<el-row type="flex">
				<ela-filter-item label="Status" :span="5">
					<el-select
						size="small"
						clearable
						v-model="filters.status"
						@change="handleFilterChange">
						<el-option value="Active">Active</el-option>
						<el-option value="Inactive">Inactive</el-option>
					</el-select>
				</ela-filter-item>
				<ela-filter-item label="Search" :span="6" float="right">
					<el-input
						icon="search"
						size="small"
						v-model="filters.search"
						@click="handleFilterChange"
						@keyup.native.enter="handleFilterChange">
					</el-input>
				</ela-filter-item>
			</el-row>
		</div>

		<el-table
			:data="brands.nodes"
			style="width: 100%"
			stripe border
			:default-sort="defaultSort"
			@sort-change="handleSortChange"
			v-loading="loadingSelfData">
			<el-table-column prop="id" label="Id" width="60" sortable></el-table-column>
			<el-table-column prop="name" label="Name"></el-table-column>
			<el-table-column prop="status" label="Status" width="100"></el-table-column>
		</el-table>

		<div slot="foot">
			<div class="footer-right">
				<el-pagination
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page="filters.page"
					:page-sizes="[20, 50, 100, 250, 500]"
					:page-size="filters.count"
					layout="total, sizes, prev, pager, next, jumper"
					:total="brands.totalCount">
			    </el-pagination>
			</div>
		</div>
	</ela-content-layout>
</template>

<script>
import {paginationMixin} from 'vutils';

export default {
	name: 'Brands',
	mixins: [
		paginationMixin(),
	],

	data() {
		return {
			brands: {},
			filters: {
				search: '',
				status: '',
				sort: '',
				order: '',
				page: 1,
				count: 20,
			},
		};
	},

	methods: {
		loadSelfData(filters) {
			return this.$api.getBrands(filters).then((brands) => {
				this.brands = brands;
			});
		},
	},

	events: {
		brandMutated() {
			this.reloadSelfData();
		},
	},
};
</script>

Filters

round(precision)

Round a number.

formatNumber(precision)

Format a number using commas and all.

Helpers

remove(arr, index)

Remove an element from an array (vue compatible).
This is reactive, means it notifies vue of changes in the array.
index can be the index of the element or a function which returns true for the elements to be removed.

update(arr, index, value)

Replace an element in an array (vue compatible).
This is reactive, means it notifies vue of changes in the array.
index can be the index of the element or a function which returns true for the element to be replaced.

addOrUpdate(arr, value, func)

Replace an element in an array (vue compatible) or adds it if it doesn't exist.
This is reactive, means it notifies vue of changes in the array.
value is the value which replaces the current value. func is a function which returns true for the element to be replaced, or it can be a property name by which search will be performed. By default it is id.

handleGraphqlRequest(request)

Given a request promise, this function will handle the incoming response from a graphql server and return a promise which handles errors appropriately.

It'll merge all fields from the fields key of errors array and put them in userErrors key of response.

import Axios from 'axios';
import startsWith from 'lodash/startsWith';
import {handleGraphqlRequest} from 'gqutils';

const GRAPHQL_ENDPOINT = '/api';

function graphqlReqest(query) {
	return handleGraphqlRequest(Axios.post(GRAPHQL_ENDPOINT, {query}));
}

function query(graphqlQuery) {
	if (!startsWith(graphqlQuery, 'query')) {
		graphqlQuery = `query { ${graphqlQuery} }`;
	}

	return graphqlReqest(graphqlQuery);
}

function mutation(graphqlQuery) {
	if (!startsWith(graphqlQuery, 'mutation')) {
		graphqlQuery = `mutation { ${graphqlQuery} }`;
	}

	return graphqlReqest(graphqlQuery);
}