Skip to content

Instantly share code, notes, and snippets.

View ryasmi's full-sized avatar
🐿️
Checks emails once per day Mon-Fri

Ryan Smith ryasmi

🐿️
Checks emails once per day Mon-Fri
View GitHub Profile
@ryasmi
ryasmi / treeToggler.js
Last active November 8, 2017 14:53
Toggles items in a tree. Toggling on selects all children and all parents (but not siblings). Toggling off deselects all children (but not parents or siblings).
item4 = {id: 4, children: []};
item3 = {id: 3, children: [], parent: 2};
item2 = {id: 2, children: [item3], parent: 1};
item1 = {id: 1, children: [item2]};
items = [item1, item4];
flatten = (items) => {
return items.reduce((flatItems, item) => {
const children = _.get(item, 'children', []);
const flatChildren = children.length === 0 ? [] : flatten(children);
return [...flatItems, item, ...flatChildren];
pad = (num) => {
return num < 10 ? `0${num}` : num.toString();
};
getIsoDate = (date) => {
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const monthDay = pad(date.getDate());
return `${year}-${month}-${monthDay}`;
};
getIsoTime = (date) => {
@ryasmi
ryasmi / generateVersionFile.sh
Last active October 8, 2017 20:33
A shell script to generate the Moodle plugin version file using git tags and Travis.
release=$(echo "${TRAVIS_TAG//v}")
requires=$REQUIRED_MOODLE_VERSION
today=$(date +"%Y-%m-%d")
todaysReleases=$(git log --tags --simplify-by-decoration --pretty="format:%ai %d" | sort -r | grep "$today" | wc -l | tr -d '[:space:]')
versionPrefix=$(date +"%Y%m%d")
versionSuffix=$(printf %02d $todaysReleases)
version="$versionPrefix$versionSuffix"
<?php
function getIn($obj, $keys, $default = null) {
$key = $keys[0];
$rest = array_slice($keys, 1);
if ($obj instanceof StdClass) {
if (isset($obj->{$key})) {
if (count($rest) > 0) {
return getIn($obj->{$key}, $rest);
} else {
@ryasmi
ryasmi / modr.js
Last active February 15, 2017 16:33
export type Path = string[];
export type Modifier = (data: any, path: Path) => any;
export type Schema = { [key: string]: Modifier };
export const defaultValue = (value: any): Modifier =>
(data) =>
data === undefined ? value : data;
export const overrideValue = (value: any): Modifier => () => value;
@ryasmi
ryasmi / AutomaticDocumentation.js
Created December 31, 2016 11:27
Uses typedoc's json output to produce documentation in markdown.
const fs = require('fs');
const docTokens = require('./ts.json');
const renderToken = token => {
const functionKind = 64;
switch (token.kind) {
case functionKind: {
const source = token.sources && renderSources(token.sources);
const sig = renderSignatures(token.signatures);
injectScript = (src) => {
return new Promise((resolve) => {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = src;
script.onload = () => resolve(window.gapi);
document.body.appendChild(script);
});
};
@ryasmi
ryasmi / LazyEvaluation.js
Last active October 7, 2016 11:32
Example of lazy evaluation in JavaScript?
add = function (a) {
return function (b) {
return function () {
a = a();
b = b();
return a + b;
};
};
};
one = function () {return 1;};
var copy = function (text) {
var range = document.createRange();
var elem = document.createElement('div');
var parent = document.createElement('div');
window.getSelection().removeAllRanges();
elem.innertText = text;
parent.appendChild(elem);
range.selectNode(elem);
window.getSelection().addRange(range);
document.execCommand('copy');
!function () {
var parseNum = function (elem) {
return parseInt(elem.innerText.replace('$', '').replace(',', ''))
};
var attrs = $('#squad > thead > tr > th').length;
var player_elems = $('#squad > tbody > tr.highlighted').toArray();
if (player_elems.length == 0) {
player_elems = $('#squad > tbody > tr').toArray();
}