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

Add the ability to namespace vue2-filters #55

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ module.exports = {
</ul>
```

## Namespacing

This package adds a set of filters and methods to every Vue component. If you are using `vue2-filters` in combination with other 3rd party components you may run into a collision between these methods and props or methods used by those components. To avoid this, it is possible to namespace the filters and methods added by `vue2-filters`. To do so, change your installation code as follows:


```javascript
import Vue from 'vue'
import { NamespacedVue2Filters } from 'vue2-filters'


Vue.use(NamespacedVue2Filters('VF'))
```

This will add the string you pass in (in the example above `VF`) to be prepended to every filter and method name. E.g. `VForderBy` and `VFcapitalize`.

## Contribution

If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/freearhey/vue2-filters/issues) or a [pull request](https://github.com/freearhey/vue2-filters/pulls).
Expand Down
47 changes: 28 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@ import * as stringFilters from './string/index'
import * as arrayFilters from './array/index'
import * as otherFilters from './other/index'

var Vue2Filters = {
install: function(Vue) {
util.each(stringFilters, function(value, key) {
Vue.filter(key, value)
})

util.each(otherFilters, function(value, key) {
Vue.filter(key, value)
})

Vue.mixin({
methods: {
limitBy: arrayFilters.limitBy,
filterBy: arrayFilters.filterBy,
orderBy: arrayFilters.orderBy,
find: arrayFilters.find
}
})
}
var install = function(Vue, namespace = '') {
util.each(stringFilters, function(value, key) {
Vue.filter(namespace + key, value)
})

util.each(otherFilters, function(value, key) {
Vue.filter(namespace + key, value)
})

Vue.mixin({
methods: {
[namespace + 'limitBy']: arrayFilters.limitBy,
[namespace + 'filterBy']: arrayFilters.filterBy,
[namespace + 'orderBy']: arrayFilters.orderBy,
[namespace + 'find']: arrayFilters.find
}
})
}

export var Vue2Filters = {
install: (Vue) => install(Vue)
}

export function NamespacedVue2Filters(namespace) {
return {
install: (Vue) => install(Vue, namespace)
};
}

export default Vue2Filters;


if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(Vue2Filters);
}
68 changes: 68 additions & 0 deletions test/mixins.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Vue2Filters, NamespacedVue2Filters } from '../src/index';

class VueStub {

constructor() {
this.filters = {};
this.methods = {};
}

filter(key, value) {
this.filters[key] = value;
}

mixin(object) {
this.methods = object.methods;
}

use(initializer) {
initializer.install(this);
}
};

describe('basic include', function() {
beforeAll(function() {
this.Vue = new VueStub();
this.Vue.use(Vue2Filters);
});

it('has all expected filters', function() {
expect(typeof this.Vue.filters.capitalize).toEqual('function');
expect(typeof this.Vue.filters.uppercase).toEqual('function');
expect(typeof this.Vue.filters.lowercase).toEqual('function');
expect(typeof this.Vue.filters.placeholder).toEqual('function');
expect(typeof this.Vue.filters.truncate).toEqual('function');
expect(typeof this.Vue.filters.currency).toEqual('function');
expect(typeof this.Vue.filters.pluralize).toEqual('function');
});

it('has all expected methods', function() {
expect(typeof this.Vue.methods.limitBy).toEqual('function');
expect(typeof this.Vue.methods.filterBy).toEqual('function');
expect(typeof this.Vue.methods.find).toEqual('function');
expect(typeof this.Vue.methods.orderBy).toEqual('function');
});
});
describe('namespaced include', function() {
beforeAll(function() {
this.Vue = new VueStub();
this.Vue.use(NamespacedVue2Filters('vf'));
});

it('has all expected filters with VF namespace', function() {
expect(typeof this.Vue.filters.vfcapitalize).toEqual('function');
expect(typeof this.Vue.filters.vfuppercase).toEqual('function');
expect(typeof this.Vue.filters.vflowercase).toEqual('function');
expect(typeof this.Vue.filters.vfplaceholder).toEqual('function');
expect(typeof this.Vue.filters.vftruncate).toEqual('function');
expect(typeof this.Vue.filters.vfcurrency).toEqual('function');
expect(typeof this.Vue.filters.vfpluralize).toEqual('function');
});

it('has all expected methods', function() {
expect(typeof this.Vue.methods.vflimitBy).toEqual('function');
expect(typeof this.Vue.methods.vffilterBy).toEqual('function');
expect(typeof this.Vue.methods.vffind).toEqual('function');
expect(typeof this.Vue.methods.vforderBy).toEqual('function');
});
});