Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / clojure-derive-multimethods-example.clj
Last active January 15, 2024 18:45
Example of Clojure Derive/Multimethods for inheritance/polymorphism
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; BASE CLASS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; public class ContentItem {
; public String foobar() {
; return "content_item";
; }
; }
@peter
peter / json
Last active January 27, 2023 11:11
Simple node script that provides a jq alternative for processing JSON on the command line
#!/usr/bin/env node --max-old-space-size=4096
// Simple node script that provides a jq alternative for processing JSON on the command line.
// Supports newline separated JSON (JSON lines) as well as JSON data.
// Also supports log data where some lines are JSON and some aren't and log lines where the
// beginning of the line is text and the end of the line is JSON, i.e.:
//
// 2022-10-18T14:07:53.960Z [INFO ] starting server with config: {"port":3000}
//
// USAGE:
@peter
peter / rack-rest-api-example.rb
Created January 31, 2013 19:06
An example of how to write a simple JSON REST API with a simplistic router directly on top of Rack (i.e. without the use of a framework like Sinatra).
#################################################################
#
# File: lib/api.rb
#
#################################################################
require 'user' # ActiveRecord model
# Connect to the db
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
@peter
peter / to-json-schema.js
Created October 6, 2021 09:17
Node script to geneare a JSON schema from JSON data
#!/usr/bin/env node
const fs = require('fs')
// NOTE: this package didn't work for me
// const toJsonSchema = require('to-json-schema')
var stringify = require('json-stable-stringify')
const Ajv = require('ajv')
const ajv = new Ajv()
@peter
peter / compact-data-for-equality-check.js
Last active October 5, 2021 08:59
Compact/Normalize data (omit null/empty values) for equality checks
const { isEmpty, isEqual } = require('lodash')
function deepPickBy(obj, predicate) {
if (Array.isArray(obj)) {
return obj.map((v) => deepPickBy(v, predicate));
} else if (obj && typeof obj === 'object') {
return Object.keys(obj).reduce((acc, key) => {
const v = obj[key]
if (v && typeof v === 'object' && Object.keys(v).length > 0) {
acc[key] = deepPickBy(v, predicate);
@peter
peter / json-to-json-schema.js
Created October 1, 2021 13:06
JSON to JSON Schema Script
#!/usr/bin/env node
const fs = require('fs')
// NOTE: this package didn't work for me
// const toJsonSchema = require('to-json-schema')
var stringify = require('json-stable-stringify')
const Ajv = require('ajv');
const ajv = new Ajv();
@peter
peter / recursive_struct.rb
Created March 26, 2014 16:09
Using a recursive struct in Ruby to honor the Uniform Access Principle when accessing data from hashes/structs/objects
# Simple wrapper to allow hashes to be accessed via dot notation recursively.
# Recurses over hashes and arrays. Works with string keys
# and symbol keys - other types of keys are not supported and
# all keys must be of the same type. Write access is only supported via
# []= Hash syntax. Supports accessing hash values with square bracket Hash syntax ([...])
# and access is indifferent to if the key is given as a string or a symbol.
# Supports JSON generation.
#
# Dependencies: Ruby.
#
@peter
peter / s3-upload
Last active August 6, 2021 05:49
File upload from Node.js to Amazon S3 with the REST API and the minimal aws4 npm package
#!/usr/bin/env node
// This script uploads a local file to S3
// Depends on the aws4 npm package for AWS signatures
// Usage:
// AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... s3-upload
const fs = require('fs')
const https = require('https')
const aws4 = require('aws4') // Handles complex AWS signatures: http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
@peter
peter / javascript-loop-performance
Created May 7, 2021 12:21
javascript-loop-performance.js
#!/usr/bin/env node
// Figure out how performant different looping constructs in JavaScript are.
// Sample output:
// for-of: elapsed.stats={"min":26,"max":59,"avg":33.13} elapsed=26,26,27,28,29,30,30,30,30,30,30,30,30,30,31,31,31,31,31,31,31,31,31,31,31,31,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,35,35,35,35,35,35,35,35,36,36,36,36,36,36,36,37,38,40,47,59
// forEach: elapsed.stats={"min":34,"max":77,"avg":56.53} elapsed=34,52,52,52,52,53,53,53,53,53,53,53,53,53,53,53,53,53,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,57,57,57,57,57,57,57,57,57,57,58,58,58,58,59,59,59,59,59,60,60,61,61,63,63,65,67,69,70,70,72,73,77
// map: elapsed.stats={"min":38,"max":76,"avg":62.48} elapsed=38,58,58,58,58,58,58,58,58,58,58,58,58,58,59,59,59,59,59,59,59,59,59,59,59,59,59,60,60,60,60,60,60,60,60,60,61,61,
// See: https://medium.com/@promentol/cryptography-for-javascript-node-js-developers-part-1-hash-function-86d119c7304
function hash(data, digest = 'hex') {
return require('crypto').createHash('sha256').update(data).digest(digest)
}
// ('foo', 10) => 6
// ('bar', 10) => 6
// ('zz', 10) => 2
// ('foo', 6) => 2