Moment, markdown and truncate filters

merge-requests/154/head
Eliot Berriot 2018-02-22 22:02:19 +01:00
rodzic 999198b1c5
commit 7ffff90005
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
4 zmienionych plików z 69 dodań i 0 usunięć

Wyświetl plik

@ -20,9 +20,11 @@
"js-logger": "^1.3.0",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.4",
"moment": "^2.20.1",
"moxios": "^0.4.0",
"raven-js": "^3.22.3",
"semantic-ui-css": "^2.2.10",
"showdown": "^1.8.6",
"vue": "^2.3.3",
"vue-lazyload": "^1.1.4",
"vue-router": "^2.3.1",

Wyświetl plik

@ -0,0 +1,31 @@
import Vue from 'vue'
import moment from 'moment'
import showdown from 'showdown'
export function truncate (str, max, ellipsis) {
max = max || 100
ellipsis = ellipsis || '…'
if (str.length <= max) {
return str
}
return str.slice(0, max) + ellipsis
}
Vue.filter('truncate', truncate)
export function markdown (str) {
const converter = new showdown.Converter()
return converter.makeHtml(str)
}
Vue.filter('markdown', markdown)
export function ago (date) {
const m = moment(date)
return m.fromNow()
}
Vue.filter('ago', ago)
export default {}

Wyświetl plik

@ -13,6 +13,7 @@ import VueLazyload from 'vue-lazyload'
import store from './store'
import config from './config'
import { sync } from 'vuex-router-sync'
import filters from '@/filters' // eslint-disable-line
sync(store, router)

Wyświetl plik

@ -0,0 +1,35 @@
import {truncate, markdown, ago} from '@/filters'
describe('filters', () => {
describe('truncate', () => {
it('leave strings as it if correct size', () => {
const input = 'Hello world'
let output = truncate(input, 100)
expect(output).to.equal(input)
})
it('returns shorter string with character', () => {
const input = 'Hello world'
let output = truncate(input, 5)
expect(output).to.equal('Hello…')
})
it('custom ellipsis', () => {
const input = 'Hello world'
let output = truncate(input, 5, ' pouet')
expect(output).to.equal('Hello pouet')
})
})
describe('markdown', () => {
it('renders markdown', () => {
const input = 'Hello world'
let output = markdown(input)
expect(output).to.equal('<p>Hello world</p>')
})
})
describe('ago', () => {
it('works', () => {
const input = new Date()
let output = ago(input)
expect(output).to.equal('a few seconds ago')
})
})
})