Skip to content

Instantly share code, notes, and snippets.

@jsonberry
jsonberry / actions.js
Last active November 9, 2018 21:23
Angular ngrx-router-store Router Actions / Effects
/* tslint:disable:max-classes-per-file */
import {Action} from '@ngrx/store';
import {NavigationExtras} from '@angular/router';
export enum RouterActionTypes {
GO = '[router] Go',
BACK = '[router] Back',
FORWARD = '[router] Forward',
}
/// Breakpoints
/// @author Jason Awbrey
/// @param { phone | tablet-portrait | tablet-landscape | desktop | desktop-big } $type [no default]
/// @return { Number } Rem calculated value for a given breakpoint
@function bp($type) {
/// @prop
$breakpoints: (phone: 599, tablet-portrait: 600, tablet-landscape: 900, desktop: 1200, desktop-big: 1800);
@if (map-has-key($breakpoints, $type) != true) {
@error "$type must be one of these: #{map-keys($breakpoints)}";
}
@jsonberry
jsonberry / helpful-javascript-utilities.js
Last active September 20, 2022 01:33
Helpful JavaScript Utilities
// zip arrays together
// Catalin Dumitru @colin_dumitru
// Jason Awbrey @jsawbrey
const zip = (a, b) => a.map((n, i) => [n, b[i]])
// merge objects in an array together
// caution: properties are overridden if duplicated
// @a [{a},{b}]
// @returns [{a, b}]
// Jason Awbrey @jsawbrey
@jsonberry
jsonberry / word-break-cross-browser.scss
Created October 24, 2017 18:26
Cross / Legacy browser support for word wrapping
.container {
word-wrap: break-word; // works for IE11, IE10
overflow-wrap: break-word; // New naming for word-wrap attribute, keep for possible deprecation of word-wrap
word-break: break-word; // Helps with non English character wrapping, supported by Blink based browsers
.item {
// display: block or inline-block may be required
max-width: 250px;
width: 100%; // Allows for word wrap in IE10/IE11
@jsonberry
jsonberry / api.service.js
Last active August 31, 2023 08:45
Angular: RxJS + Lodash for getting deeply nested data out of an Observable stream and into presentational components
// .js extension for syntax highlighting - all files are actually .ts
// A general service that makes an HTTP call to an API
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
@Injectable()
export class ApiService {
constructor (
private http: HttpClient,
@jsonberry
jsonberry / watcher.js
Created July 7, 2017 16:50
Spawn a new task!
'use strict';
/*
* Run the Metalsmith build upon hbs and json changes
* This enables live reloading of template and data changes
*/
const { watch } = require('fs');
const { spawn: run } = require('child_process');
const { resolve, extname } = require('path');
@jsonberry
jsonberry / git_stats.sh
Created May 31, 2017 16:52
Get some git project stats!
#author @StevenIrby
function git-stats() {
git log --shortstat --author="$(git config user.name)" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "Commit stats:\n- Files changed (total).. %s\n- Lines added (total).... %s\n- Lines deleted (total).. %s\n- Total lines (delta).... %s\n- Add./Del. ratio (1:n).. 1 : %s\n", files, inserted, deleted, delta, ratio }' -
}
@jsonberry
jsonberry / breakpoints.sass
Created May 16, 2017 04:22
Breakpoint mixin
=breakpoint($size)
@if $size == phone-only
@media (max-width: 599px)
@content
@else if $size == tablet-portrait-up
@media (min-width: 600px)
@content
@else if $size == tablet-landscape-up
@media (min-width: 900px)
@content
@jsonberry
jsonberry / hasEmptyProps.js
Last active February 17, 2017 23:38
Data latch utility for JSON using Lodash
/*
Sometimes I pass data into a transformation layer
I always expect the data to be a JSON object
None of the properties of the object should be undefined, null, empty objects, or empty arrays
If any of them are, then eject out of the transformation so the app doesn't have a chance to explode
*/
// How to implement
function transformData(data) {
@jsonberry
jsonberry / es6_scoped_decl.md
Last active February 13, 2017 19:17
Override const declaration with function scope and let

You can effectively "override" a const if the subsequent declaration is in a nested function scope.

You can also override a const in a block with let, as that will keep the declaration lexically scoped to that block

Example: The JS engine will not allow an override of the const, even though the var is in a nested block scope

function myScopedConst() {
    const myScopedConst = 42;
    console.log('myScopedConst:: before the block ->', myScopedConst);