Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@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 / 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 / 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
@peter
peter / terraform-util.js
Created December 17, 2020 11:30
De-duplicating Terraform code by generating it from JavaScript
const fs = require('fs')
module.exports = {
writeTerraform,
generateMain,
}
// Templates are just terraform (.tf files) with the following kinds of interpolations:
// 1. Variables embedded in a string: "terraform-states-<id>"
// 2. Variables occupying an entire string: "<apiId>".
@peter
peter / traverse-object-generator.js
Last active December 11, 2020 12:10
Traverse an object in Node.js/Javascript with a generator function
function isObject (value) {
return value != null && typeof value === 'object' && value.constructor === Object
}
//////////////////////////////////////////
// GENERATOR
//////////////////////////////////////////
function * traverseObj (value, path = []) {
@peter
peter / check-linux-version.sh
Created December 10, 2020 09:21
Check Linux Version
#!/bin/bash
uname -a
if test -f "/etc/os-release"; then
cat /etc/os-release
fi
if command -v lsb_release; then
lsb_release -a
fi
cat /proc/version
@peter
peter / cors-proxy.js
Last active August 24, 2020 12:33
Local Node http proxy with CORS headers
// The use case is that you are developing a web app locally that makes Ajax requests to some external
// API that doesn't have sufficiently permissive CORS headers so your requests fail. The solution is to point the app
// to this local proxy which will allow CORS.
const http = require('http')
const axios = require('axios')
const HOSTNAME = '127.0.0.1'
const PROXY_BASE_URL = process.env.PROXY_BASE_URL;
const PORT = process.env.PORT || 9999