Skip to content

Instantly share code, notes, and snippets.

@danielrw7
danielrw7 / deep_extend.py
Created March 23, 2023 18:27
deep_extend.py
def deep_extend(base, arg):
if not (isinstance(base, dict) and isinstance(arg, dict)):
return arg
return dict([
*base.items(),
*map(
lambda item: (
item[0],
deep_extend(base.get(item[0], None), item[1])),
@danielrw7
danielrw7 / deepExtend.js
Last active March 23, 2023 18:28
deepExtend.js
function deepExtend(base, arg) {
const baseType = typeof base
const argType = typeof arg
if (baseType !== argType || baseType !== 'object' || arg instanceof Array) {
return arg
}
return Object.fromEntries([
...Object.entries(base),
@danielrw7
danielrw7 / appendableSetGenerator.ts
Last active February 22, 2023 13:32
add items to a generator while looping through each unique element of it
function appendableSetGenerator<T>(initialToTry?: IterableIterator<T>, tried = new Set<T>()): [Generator<T>, (...values: T[]) => void] {
const toTry = new Set<T>(initialToTry || null)
return [
(function* () {
while (toTry.size) {
for (const value of toTry.values()) {
tried.add(value)
toTry.delete(value)
yield value
break
import sanitizeHTML from 'sanitize-html'
const wysiwygSel = '.wysiwyg'
const allowedTags = [
'b',
'i',
'u',
'br',
]
@danielrw7
danielrw7 / NumberInput.vue
Last active June 6, 2019 14:52
Vue NumberInput component allowing for shift + arrow key to jump up and down by a specified amount
<template>
<input type="number" @keydown.native="onKeydown" :value="value" @input="onInput" :min="min" :max="max" :step="step" v-bind="$attrs" />
</template>
<script>
export default {
name: 'NumberInput',
props: {
value: {
type: Number,
@danielrw7
danielrw7 / roundPadded.js
Created February 15, 2019 16:19
Round padded
function roundPadded(n, d = 0) {
const div = Math.pow(10, d)
let res = (Math.round(n * div) / div).toString().split('.')
const whole = res[0]
if (d <= 0) {
return whole
}
let dec = res[1] || ''
if (d > 0 && dec.length < d) {
dec += '0'.repeat(d - dec.length)
@danielrw7
danielrw7 / jsonToCsv.js
Created June 25, 2018 15:17
jsonToCsv.js
function jsonToCsv(headers, rows, rowSep="\r\n") {
function escape(value) {
return `"${value.toString().replace('"', '""')}"`
}
function row(values) {
return values.map(escape).join(',').replace(/(,"")+$/, '')
}
return row(headers) + rowSep + rows.map(row => {
return headers.map(header => row[header] || '')
}).map(row).join(rowSep)
@danielrw7
danielrw7 / Ownable.sol
Created June 16, 2017 23:05
First draft of a generic tradeable contract
pragma solidity ^0.4.11;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
@danielrw7
danielrw7 / SafeQuery.php
Last active June 8, 2017 15:26
PDO safe query generation
<?php
// Args:
// SafeQuery::safe_query($table, $select_fields = array("*"), $where = array(), $order_by = array(), $limit = array(0,100))
$ops = array(
"equals" => array("="),
"not_equals" => array("!="),
"greater_than" => array(">"),
"greater_than_or_equal" => array(">="),
"less_than" => array("<"),