Skip to content

Instantly share code, notes, and snippets.

View ndabAP's full-sized avatar
🔭

Julian Claus ndabAP

🔭
View GitHub Profile
@ndabAP
ndabAP / SomeController.js
Last active June 28, 2017 04:52
Use nested services instead of helpers within Sails.js 1.0
// api/controllers/SomeController.js
module.exports = {
/**
* @param req
* @param res
*/
create: (req, res) => {
let love = sails.services.SomeService.getLove()
@ndabAP
ndabAP / main.js
Last active August 3, 2017 10:08
Load mobile/desktop version of your Vue.js application based on browser width
// main.js is your Webpack entry point. "bootstrap.desktop" and "bootstrap.mobile" load dependencies based on the browser width
/**
* @see {@link https://stackoverflow.com/questions/1038727/how-to-get-browser-width-using-javascript-code#1038781}
*/
const browserWidth = Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
@ndabAP
ndabAP / test.spec.js
Last active April 25, 2019 11:43
Mock computed property of Vue.js mixin
import Vue from 'vue'
import 'babel-polyfill'
import Component from '~/components/Component'
import {
mount
} from 'vue-test-utils'
import sinon from 'sinon'
import {
assert
} from 'chai'
@ndabAP
ndabAP / app.js
Created October 24, 2017 14:16
Creating async functions in JavaScript
const a = () => {
return new Promise(resolve => {
console.log('a')
resolve(1)
})
}
const b = () => {
return new Promise(resolve => {
console.log('b')
@ndabAP
ndabAP / random.js
Last active June 26, 2018 07:17
Random alphabetical string with JavaScript
/**
* @see {@url https://stackoverflow.com/questions/30452263/is-there-a-mechanism-to-loop-x-times-in-es6-ecmascript-6-without-mutable-varia#answer-30452949}
*/
const times = x => f => {
if (x > 0) {
f()
times (x - 1) (f)
}
}
@ndabAP
ndabAP / User.vue
Created November 3, 2017 08:51
Decorators in Vue.js
<template>
<div>
<!-- User form -->
</div>
</template>
<script>
import User from 'src/decorators/UserDecorator'
export default {
@ndabAP
ndabAP / index.js
Last active January 14, 2018 08:50
How to use Lodash's functional programs functions within a regular pattern
/**
* This way, you can import the functional program function and use in two ways. You don't have to fear any name collisions.
*/
let element = [{key: 1, value: a}, {key: 2, value: b}]
// Functional program usage
flow([
map(element => omit(['key']))
])(elements)
@ndabAP
ndabAP / App.vue
Last active November 22, 2021 00:42
Vue.js pluralize filter
<template>
<div>
<p>I got {{ amount }} {{ 'cookie' | pluralize(amount) }}</p>
<button @click="decrement">Decrement</button>
</div>
</template>
<script>
export default {
data: () => ({
@ndabAP
ndabAP / getRelativeDays.js
Last active June 10, 2018 05:47
Return days relative to first day
import moment from 'moment'
import flow from 'lodash/flow'
import map from 'lodash/fp/map'
/**
* Returns days relative to first one, e. g.: 0, 2, 9, 10, 11
*
* @params {array} dates
* @returns {array}
*/
@ndabAP
ndabAP / functions.js
Last active June 5, 2018 12:45
Vue.js case
// It might be that you need components loaded dynamically. You can use the following function
const getComponentName = (identifier, method = 'post') => {
return `${identifier.replace(/([A-Z])/g, '-$1').toLowerCase().replace(/^-(.*)$/, '$1')}-${method}`
}
// Example: getComponentName('PriceTable', 'get') yields 'price-table-get'