README
JS Helpers, by Billow
A useful set of JS helpers for Billow's Vue apps. Includes a component mixin, a bunch of filters, and string utilities.
Installation
npm install @billow/js-helpers
Query String mixin
Used for managing tabs through window history, the query-string mixin is obtainable from helpers.mixins.queryString. In your component:
import { mixins } from '@billow/js-helpers'
export default {
mixins: [
mixins.queryString,
],
}
To set a tab, call this.setTab(tabName). The active tab can be obtained from this.activeTab.
Note that this mixin relies on a Vuex store being injected, and interacts with a namespaced module called
app. This module must have a mutator namedsetTabwhich accepts the name of a tab and mutates it in state, as well as a getter namedtab, which simply returns the name of the tab from wherever it lives in state.
If you'd prefer, you can also import the mixin directly:
import queryStringMixin from '@billow/js-helpers/src/mixins/query-string'
FormData utility
If you need to leverage the FormData API to submit multipart form data via an XHR request, you can make use of this utility, which merges an object's keys and values (often from state) together, whilst retaining integrity of data that would often be lost without passing through this API, such as a File.
import { utilities } from '@billow/js-helpers'
// Somewhere in a store module, like an action:
let payload = utilities.formData({
...state.userProfile,
picture: state.profilePicture
})
// Or if your file (a picture, in this example) is in the same state key:
let payload = utilities.formData(state.userProfile)
If you'd prefer, you can also import the utility directly:
import makeFormData from '@billow/js-helpers/src/utilities/form-data'
String utility
A simple utility that currently exports a format helper, which assists in formatting a string using named keys. A filter is also available to leverage this utility.
import { utilities } from '@billow/js-helpers'
utilities.string.format('{key} equals :value', {
key: this.generateKey(),
value: this.value,
})
If you'd prefer, you can also import the utility directly:
import { format } from '@billow/js-helpers/src/utilities/string'
Filters
import { filters } from '@billow/js-helpers'
// Install the filters
Vue.use(filters)
The available filters are listed below in alphabetical order with a method signature (if available) and at least one example.
You can access filters using {{ var | filter([args]) }} in your component template, or this.$options.filters.filter(var[, args]) in your JS.
💡 Some filter-arguments are swappable, meaning that
{{ a | b(c) }}and{{ c | b(a) }}would produce the same result.
asAccounting
Arguments: (currency = 'R', precision = 2, thousandsSeparator = ',', precisionSeparator = '.', format = '%s %v')
Examples:
2000 | asAccounting // R 2,000.00
2000 | asAccounting('