Skip to content

Instantly share code, notes, and snippets.

@rockydd
rockydd / sort_script.jq
Last active January 23, 2024 22:26
A jq script for comprehensive sorting of JSON objects. It recursively sorts keys in objects, orders arrays by simple types, and sorts arrays of objects by 'name' or 'id'. Ideal for standardizing complex JSON structures for APIs, configurations, or data processing. Usage: jq -f sort_script.jq input.json with input.json as your target JSON file.
def recursive_sort:
if type == "object" then
to_entries
| sort_by(.key)
| map( {key: .key, value: (.value | recursive_sort)} )
| from_entries
elif type == "array" then
map( if type == "object" or type == "array" then . | recursive_sort else . end )
| if length == 0 or (first | type) != "object" then
sort
@rockydd
rockydd / replace.sh
Created December 2, 2020 17:33
sed in mac need to provide backup file extension
git grep --name-only app *.ts | gawk 'system("sed -i \"\" sAappA@yyy/zzzA "$1)'
for i in {0..255}; do
printf "\x1b[38;5;${i}mcolour${i}\x1b[0m\n"
done
// Example POST method implementation:
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
fib_(1,[1]).
fib_(2,[1,1]).
fib_(3, [H|[A|[B|Tail]]]) :- fib_(2, [A|[B|Tail]]), H is A + B.
fib_(N, [H|[A|[B|Tail]]]) :- N2 is N - 1, fib_(N2, [A|[B|Tail]]), H is A + B.
fib(N, List) :- fib_(N, List0), reverse(List0, List).
import { fromEvent, interval } from 'rxjs';
import { tap, throttle } from 'rxjs/operators';
const clicks = fromEvent(document, 'click');
//const result = clicks.pipe(throttle(ev => interval(1000)));
const result = interval(2000).pipe(tap(x => console.log('v:', x)), throttle( i => clicks, {leading: true, trailing: true}));
result.subscribe(x => console.log(x));
import { fromEvent, interval } from 'rxjs';
import { throttle } from 'rxjs/operators';
const clicks = fromEvent(document, 'click');
//const result = clicks.pipe(throttle(ev => interval(1000)));
const result = interval(2000).pipe(throttle( i => clicks));
result.subscribe(x => console.log(x));
@rockydd
rockydd / pi.hs
Created April 7, 2019 16:33
Calculate and print pi in haskell
import Control.Monad
import System.IO
pi_ = g(1,0,1,1,3,3) where
g (q,r,t,k,n,l) =
if 4*q+r-t < n*t
then n : g (10*q, 10*(r-n*t), t, k, div (10*(3*q+r)) t - 10*n, l)
else g (q*k, (2*q+r)*l, t*l, k+1, div (q*(7*k+2)+r*l) (t*l), l+2)
.tpl_install_npm: &npm_install
before_script:
- node -v && npm -v
- npm rebuild node-sass
- npm install --unsafe-perm --no-save
.build_staging_and_upload: &upload_gui_for_staging
image: vmdev/base_node:chrome-alpine
stage: staging_build
@rockydd
rockydd / simplified_ngrx.js
Last active August 1, 2018 13:30
simplified ngrx
class Dispatcher extends Rx.Subject{
dispatch(value : any) : void {
this.next(value);
}
}
class Store extends Rx.BehaviorSubject{
constructor(
private dispatcher,
private reducer,