Skip to content

Instantly share code, notes, and snippets.

View redgeoff's full-sized avatar

Geoff Cox redgeoff

View GitHub Profile
@redgeoff
redgeoff / each-leaf.js
Last active December 31, 2020 00:31
eachLeaf - Recursively process each leaf in JavaScript object
import each from 'lodash/each';
const eachLeafInner = (collection, iteratee, key) => {
if (collection && typeof collection === 'object') {
each(collection, (value, key) => {
eachLeafInner(value, iteratee, key);
})
} else if (key !== undefined) {
iteratee(collection, key)
}
@redgeoff
redgeoff / new-function-no-local-scope.js
Last active December 29, 2020 22:01
JavaScript New Function Only Has Access to Global Scope
// The following code proves that functions created in `new Function()` cannot access local scope
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function for more details
let userDefinedFunction = function () {
const foo = 'bar';
const fn = () => { console.log(foo) };
fn(); // This succeeds and prints 'bar'
return new Function('props', `const f = ${fn.toString()}; return f(props);`)
@redgeoff
redgeoff / generate-password-hash.js
Last active February 16, 2022 16:58
Encode/decode a password in JS in a way that is compatible with Python's werkzeug password hashing
// Encode/decode a password in JS in a way that is compatible with Python's werkzeug password hashing
const crypto = require('crypto');
const util = require('util')
// Hashed value of clearTextPassword='secret'
const EXAMPLE_PYTHON_PASSWORD = 'pbkdf2:sha256:50000$GPJFgPGqrbYqxiM1DITq919diw3sRb5k$970485eef0aa61bf39dba3468b913ada19937eec00fec74b3077a7aec905f9c9'
const pbkdf2 = util.promisify(crypto.pbkdf2)
@redgeoff
redgeoff / open-api.yml
Created April 8, 2019 21:27
OpenAPI 3.0
openapi: 3.0.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
@redgeoff
redgeoff / swagger.yml
Created March 9, 2019 00:06
Example Swagger Definition
swagger: '2.0'
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
termsOfService: 'http://swagger.io/terms/'
contact:
name: Swagger API Team
license:
name: MIT
@redgeoff
redgeoff / index.js
Created November 5, 2018 17:40
Autogenerating Forms - Conditional Submit
onSubmit={({ component }) => {
// TODO: Contact some API with the data
console.log("submitting", component.getValues());
// Simulate response from API saying that email address is already in use and report this
// error to the user
if (component.get("fields.email.value") === "taken@example.com") {
component.set({ "fields.email.err": "already in use" });
} else {
// Everything was successful so redirect, show confirmation, etc...
@redgeoff
redgeoff / index.js
Created November 5, 2018 17:38
Autogenerating Forms - onMount
onMount={({ component }) => {
// Load any initial data, e.g. from an API
component.setValues({
id: "abc123",
firstName: "Bob",
lastName: "Marley",
email: "bob@example.com"
});
}}
@redgeoff
redgeoff / index.js
Created November 5, 2018 17:21
Autogenerating Forms - Render
ReactDOM.render(
<Component
definition={definition}
onSubmit={({ component }) => {
alert(JSON.stringify(component.getValues()));
}}
/>,
document.getElementById("root")
);
@redgeoff
redgeoff / index.js
Last active November 5, 2018 17:21
Autogenerating Forms - Definition
const definition = {
component: "Form",
fields: [
{
name: "heading",
component: "Text",
text: "# Form using [MSON](https://github.com/redgeoff/mson)"
},
{
name: "fullName",
@redgeoff
redgeoff / index.js
Last active November 4, 2018 04:34
MSON Talk: EventEmitters
// Instantiate with default prop
const component = new MyComponent({ firstName: 'Robert' });
// Listener that is run when lastName changes
component.on('lastName', value => { /* new lastName */ });
// Set props
component.set({ firstName: 'Bob', lastName: 'Marley' });
component.get('firstName'); // Bob