Skip to content

Instantly share code, notes, and snippets.

@pirey
pirey / knex-pagination.js
Last active April 6, 2024 14:40 — forked from andremsantos/knex-pagination.js
Adding pagination to knex.js
const config = require('./config')
const knex = require('knex')(config.db)
const QueryBuilder = require('knex/lib/query/builder')
QueryBuilder.prototype.paginate = function ({ limit = 10, page = 1 }) {
const offset = (page - 1) * limit
return Promise.all([
this.clone().count('* as count').first(),
this.offset(offset).limit(limit)
@pirey
pirey / notEmpty.js
Last active January 9, 2018 13:29
is this thing not empty?
const is = require('is_js') // require this awesome library
const notEmpty = o =>
is.not.empty(o) && Object.keys(o).map(key => {
return is.array(o[key])
? !is.all.empty(o[key])
: is.not.empty(o[key])
}).reduce((a, b) => a && b, true)
// empty examples
# This file has been auto-generated by i3-config-wizard(1).
# It will not be overwritten, so edit it as you like.
#
# Should you change your keyboard layout some time, delete
# this file and re-run i3-config-wizard(1).
#
# i3 config file (v4)
#
# Please see http://i3wm.org/docs/userguide.html for a complete reference!
# i3 config file (v4)
# Please see http://i3wm.org/docs/userguide.html for a complete reference!
# Set mod key (Mod1=<Alt>, Mod4=<Super>)
set $mod Mod1
# set default desktop layout (default is tiling)
# workspace_layout tabbed <stacking|tabbed>
# Configure border style <normal|1pixel|pixel xx|none|pixel>
const capitalize = (s) => s.toLowerCase().split(' ').map((w) => w.replace(/^\w/, (s) => s.toUpperCase())).join(' ')
@pirey
pirey / .zshrc
Created August 9, 2018 17:36
copy of my personal .zshrc
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="robbyrussell"
/*
* convert ojbect into query params format
* used for request with content-type application/x-www-form-urlencoded
*
* e.g
* { param: "someVal", param2: "otherVal" }
*
* is converted into
*
* param1=someVal&param2=otherVal
@pirey
pirey / file-input.jsx
Last active October 27, 2023 18:09
react file input, convert File to source url, useful to preview image before uploading
import {readFileAsUrl} from './readFileAsUrl'
class Example extends React.Component {
constructor() {
this.state = {
previewUrl: ''
}
}
render() {
return (
@pirey
pirey / countries.json
Last active October 8, 2018 05:45
list of country names and codes
[
{
"name": "Afghanistan",
"number": "93",
"code": "AF"
},
{
"name": "Albania",
"number": "355",
"code": "AL"
@pirey
pirey / withRefs.ts
Last active November 11, 2018 05:29
React HOC to handle refs
import { withStateHandlers } from 'recompose'
import { ReactNode } from 'react';
type Ref = ReactNode
type RefsState = {}
type RefsHandlers = {
setRef: (fieldName: string, ref: Ref) => undefined
focusRef: (fieldName: string) => undefined
}
export type RefsProps = RefsHandlers & RefsState