vue-computed-helpers

Computed helpers for Vue apps
3,143 284
Install
npm install vue-computed-helpers
Latest Version:1.3.0
License:MIT
Last Updated:May 16, 2018
Links: GitHub  ·  npm
Maintainer: michalsnik

vue-computed-helpers

NPM version Build Status

This package contains bunch of useful helpers that can be used to simplify computed properties

:cd: Installation

Via npm:

npm install vue-computed-helpers --save

Via yarn:

yarn add vue-computed-helpers

:rocket: Usage

import { eq, count, countBy } from 'vue-computed-helpers'

export default {
  data() {
    return {
      todos: [
        { title: 'Clean house', done: false },
        { title: 'Buy beer', done: true },
        { title: 'Watch GoT', done: true }
      ]
    }
  },
  computed: {
    allTodosCount: count('todos'), // 3
    completedCount: countBy('todos', 'done', true), // 2
    allCompleted: eq('completedCount', 'allTodosCount') // false
  }
}

:star: Helpers

Helper Usage Variable argument count allowed
eq eq('anyProperty', x) no
notEq notEq('anyProperty', x) no
not not('anyProperty', 'anotherProp', ...) yes
and and('anyProperty', 'anotherProp', ...) yes
or or('anyProperty', 'anotherProp', ...) yes
xor xor('anyProperty', 'anotherProp') no
gt gt('numProperty', x) no
gte gte('numProperty', x) no
lt lt('numProperty', x) no
lte lte('numProperty', x) no
sum sum('numProperty', x, ...) yes
alias alias('anyProperty') no
bool bool('anyProperty') no
empty empty('anyProperty') no
min min('arrayProperty') no
max max('arrayProperty') no
filter filter('arrayProperty', (el) => el.done === true) no
filterBy filterBy('arrayProperty', 'done', true) no
find find('arrayProperty', (el) => el.id === 5) no
findBy findBy('arrayProperty', 'id', 5) no
map map('arrayProperty', (el) => el.id) no
mapBy mapBy('arrayProperty', 'id') no
count count('arrayProperty') no
countBy countBy('arrayProperty', 'done', true) no
classObject classObject('isPrimary', 'has-title:title', 'wide') yes

x means that it can be either value or property name. If you provide a string and there will be a property with that name it's value will be used to perform the check.

:mag: Detailed usage of some helpers

classObject

Example code:

import { classObject } from 'vue-computed-helpers'

export default {
  props: ['isPrimary', 'title', 'wide']
  computed: {
    classObj: classObject('isPrimary', 'has-title:title', 'wide')
  }
}

Given all above props are set and truthy, this computed property will return the following object:

{
  'is-primary': true,
  'has-title': true,
  'wide': true
}

Which can be used in template:

<template>
  <div :class="classObj">
  </div>
</template>

:lock: License

See the LICENSE file for license rights and limitations (MIT).

Related Packages

@21cnfe/onekit-mp-cli

小程序框架开发工具

1,154
helpers-plugin

A typescript plugin that adds helpers for Array, Object, and other prototypes.

655
duplicate-dependencies-webpack-plugin

webpack plugin to find duplicated dependencies from your bundle

332,104 6
@debut/plugin-report

Report generation for strategy backtesting in debut

4,011 18
@amilochau/core-vue3

Core plugin for vue.js (v3) projects

3,802 0

Version History

Version Released License
1.3.0 MIT
1.2.0 MIT
1.1.0 MIT
1.0.0 MIT