Export modules from utils

This commit is contained in:
An Phan 2016-04-10 16:51:06 +07:00
parent c9ebda016a
commit 0cb5ae265f
8 changed files with 95 additions and 97 deletions

View file

@ -25,7 +25,7 @@
<script>
import settingStore from '../../../stores/setting';
import utils from '../../../services/utils';
import { parseValidationError } from '../../../services/utils';
export default {
data() {
@ -48,7 +48,7 @@
let msg = 'Unknown error.';
if (error.status === 422) {
msg = utils.parseValidationError(error.data)[0];
msg = parseValidationError(error.data)[0];
}
this.$root.showOverlay(`Error: ${msg}`, 'error', true);

View file

@ -69,7 +69,7 @@
<script>
import { every } from 'lodash';
import utils from '../../services/utils';
import { br2nl } from '../../services/utils';
import artistStore from '../../stores/artist';
import albumStore from '../../stores/album';
import songStore from '../../stores/song';
@ -197,11 +197,11 @@
songStore.getInfo(this.songs[0], () => {
this.loading = false;
this.formData.lyrics = utils.br2nl(this.songs[0].lyrics);
this.formData.lyrics = br2nl(this.songs[0].lyrics);
this.formData.track = this.songs[0].track;
});
} else {
this.formData.lyrics = utils.br2nl(this.songs[0].lyrics);
this.formData.lyrics = br2nl(this.songs[0].lyrics);
this.formData.track = this.songs[0].track;
}
} else {

View file

@ -45,8 +45,8 @@
import $ from 'jquery';
import rangeslider from 'rangeslider.js';
import { isAudioContextSupported } from '../../services/utils';
import equalizerStore from '../../stores/equalizer';
import utils from '../../services/utils';
export default {
data() {
@ -210,7 +210,7 @@
events: {
'equalizer:init': function (player) {
if (utils.isAudioContextSupported()) {
if (isAudioContextSupported()) {
this.init(player);
}
},

View file

@ -73,7 +73,7 @@
<script>
import config from '../../config';
import playback from '../../services/playback';
import utils from '../../services/utils';
import { isAudioContextSupported } from '../../services/utils';
import songStore from '../../stores/song';
import favoriteStore from '../../stores/favorite';
@ -97,7 +97,7 @@
*
* @type {Boolean}
*/
useEqualizer: utils.isAudioContextSupported(),
useEqualizer: isAudioContextSupported(),
};
},

View file

@ -1,83 +1,81 @@
import isMobile from 'ismobilejs';
export default {
/**
* Convert a duration in seconds into H:i:s format.
* If H is 0, it will be ommited.
*/
secondsToHis(d) {
d = parseInt(d, 10);
/**
* Convert a duration in seconds into H:i:s format.
* If H is 0, it will be ommited.
*/
export function secondsToHis(d) {
d = parseInt(d, 10);
let s = d % 60;
let s = d % 60;
if (s < 10) {
s = '0' + s;
}
let i = Math.floor((d / 60) % 60);
if (i < 10) {
i = '0' + i;
}
let h = Math.floor(d / 3600);
if (h < 10) {
h = '0' + h;
}
return (h === '00' ? '' : h + ':') + i + ':' + s;
},
/**
* Parse the validation error from the server into a flattened array of messages.
*
* @param {Object} error The error object in JSON format.
*
* @return {Array.<String>}
*/
parseValidationError(error) {
return Object.keys(error).reduce((messages, field) => messages.concat(error[field]), []);
},
/**
* Check if AudioContext is supported by the current browser.
*
* @return {Boolean}
*/
isAudioContextSupported() {
// Apple device just doesn't love AudioContext that much.
if (isMobile.apple.device) {
return false;
}
const AudioContext = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
if (!AudioContext) {
return false;
}
// Safari (MacOS & iOS alike) has webkitAudioContext, but is buggy.
// @link http://caniuse.com/#search=audiocontext
if (!(new AudioContext()).createMediaElementSource) {
return false;
}
return true;
},
/**
* Turn <br> into new line characters.
*
* @param {string} str
*
* @return {string}
*/
br2nl(str) {
return str.replace(/<br\s*[\/]?>/gi, '\n');
if (s < 10) {
s = '0' + s;
}
let i = Math.floor((d / 60) % 60);
if (i < 10) {
i = '0' + i;
}
let h = Math.floor(d / 3600);
if (h < 10) {
h = '0' + h;
}
return (h === '00' ? '' : h + ':') + i + ':' + s;
};
/**
* Parse the validation error from the server into a flattened array of messages.
*
* @param {Object} error The error object in JSON format.
*
* @return {Array.<String>}
*/
export function parseValidationError(error) {
return Object.keys(error).reduce((messages, field) => messages.concat(error[field]), []);
};
/**
* Check if AudioContext is supported by the current browser.
*
* @return {Boolean}
*/
export function isAudioContextSupported() {
// Apple device just doesn't love AudioContext that much.
if (isMobile.apple.device) {
return false;
}
const AudioContext = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
if (!AudioContext) {
return false;
}
// Safari (MacOS & iOS alike) has webkitAudioContext, but is buggy.
// @link http://caniuse.com/#search=audiocontext
if (!(new AudioContext()).createMediaElementSource) {
return false;
}
return true;
};
/**
* Turn <br> into new line characters.
*
* @param {string} str
*
* @return {string}
*/
export function br2nl(str) {
return str.replace(/<br\s*[\/]?>/gi, '\n');
};

View file

@ -10,7 +10,7 @@ import {
orderBy
} from 'lodash';
import utils from '../services/utils';
import { secondsToHis } from '../services/utils';
import stub from '../stubs/album';
import songStore from './song';
import artistStore from './artist';
@ -84,7 +84,7 @@ export default {
*/
getLength(album) {
Vue.set(album, 'length', reduce(album.songs, (length, song) => length + song.length, 0));
Vue.set(album, 'fmtLength', utils.secondsToHis(album.length));
Vue.set(album, 'fmtLength', secondsToHis(album.length));
return album.fmtLength;
},

View file

@ -2,7 +2,7 @@ import Vue from 'vue';
import { without, map, take, remove, orderBy, each } from 'lodash';
import http from '../services/http';
import utils from '../services/utils';
import { secondsToHis } from '../services/utils';
import stub from '../stubs/song';
import favoriteStore from './favorite';
import sharedStore from './shared';
@ -42,7 +42,7 @@ export default {
this.all = albums.reduce((songs, album) => {
// While doing so, we populate some other information into the songs as well.
each(album.songs, song => {
song.fmtLength = utils.secondsToHis(song.length);
song.fmtLength = secondsToHis(song.length);
// Manually set these additional properties to be reactive
Vue.set(song, 'playCount', 0);
@ -97,7 +97,7 @@ export default {
const duration = songs.reduce((length, song) => length + song.length, 0);
if (toHis) {
return utils.secondsToHis(duration);
return secondsToHis(duration);
}
return duration;
@ -212,7 +212,7 @@ export default {
// Convert the duration into i:s
if (data.album_info && data.album_info.tracks) {
each(data.album_info.tracks, track => track.fmtLength = utils.secondsToHis(track.length));
each(data.album_info.tracks, track => track.fmtLength = secondsToHis(track.length));
}
// If the album cover is not in a nice form, don't use it.

View file

@ -1,15 +1,15 @@
require('chai').should();
import utils from '../../services/utils';
import { secondsToHis, parseValidationError } from '../../services/utils';
describe('services/utils', () => {
describe('#secondsToHis', () => {
it('correctly formats a duration to H:i:s', () => {
utils.secondsToHis(7547).should.equal('02:05:47');
secondsToHis(7547).should.equal('02:05:47');
});
it('ommits hours from short duration when formats to H:i:s', () => {
utils.secondsToHis(314).should.equal('05:14');
secondsToHis(314).should.equal('05:14');
});
});
@ -19,7 +19,7 @@ describe('services/utils', () => {
err_1: ['Foo'],
};
utils.parseValidationError(error).should.eql(['Foo']);
parseValidationError(error).should.eql(['Foo']);
});
it('correctly parses multi-level validation error', () => {
@ -28,7 +28,7 @@ describe('services/utils', () => {
err_2: ['Baz', 'Qux'],
};
utils.parseValidationError(error).should.eql(['Foo', 'Bar', 'Baz', 'Qux']);
parseValidationError(error).should.eql(['Foo', 'Bar', 'Baz', 'Qux']);
});
});
});