Skip to content

Instantly share code, notes, and snippets.

@cmstead
cmstead / argCountValidator.js
Created February 2, 2024 19:55
Decorator to validate argument count
function validateAndCall(args, enforcedFunction, maxArgumentCount) {
if(args.length > maxCount) {
throw new Error(`Function '${fn.name}' cannot be called with more than ${maxCount} arguments, but was called with ${args.length}`);
}
return fn.apply(null, args);
}
function enforceCount({ enforcedFunction, maxArgumentCount }) {
return function () {
@cmstead
cmstead / DataAccess.py
Created March 17, 2021 18:26
Python DI with PyAutoDI
from ...Interfaces.LoggerInterface import LoggerInterface
class DataAccess:
def __init__(self, logger=LoggerInterface): # connection_string_factory
self.logger = logger
def query(self, user_data):
self.validate_request(user_data)
self.logger.log(f"User provided input: {user_data}")
@cmstead
cmstead / InnerHTML.html
Last active March 3, 2021 07:21
Examples from code questions
<!-- https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML -->
<div id="container">
<p>This is some text</p>
</div>
<script>
// innerHTML retrieves all HTML content inside of a selected element
// and returns it as a string
@cmstead
cmstead / to-do-list.disc
Last active August 12, 2020 02:59
To Do List written in disc lang
begin
# To Do List
let tasks be (newArray:)
declare function addATask
let newTask be prompt: "What do you want to do?"
appendTo: tasks newTask
call startToDoList
@cmstead
cmstead / gist:7ad64539d13dfdb5a2591bc3be3c8eeb
Last active March 3, 2024 06:40
VS Code snippet transform: convert camel case to Pascal case or vice versa
// If you want to convert a PascalCase variable to camelCase, you can do the following:
// Where `n` is the tab stop you want to reference
${n/^(.)(.*)$/${1:/downcase}${2}/}
// Example: ${1:This is my original} => ${1/^(.)(.*)$/${1:/downcase}${2}/}
// If you want to convert a camelCase variable to PascalCase, you can do the following:
// Where `n` is the tab stop you want to reference
${n/^(.)(.*)$/${1:/upcase}${2}/}
@cmstead
cmstead / post-importer.js
Created March 7, 2019 22:42
Converting WordPress blog posts to Jekyll
'use strict';
const fs = require('fs');
const xml2js = require('xml2js');
const moment = require('moment');
const importConfig = require('./postImporterConfig.json');
const { xmlPath, oldDomain, newDomain, wpContentLocation } = importConfig;
const postXML = fs.readFileSync(xmlPath, { encoding: 'utf8' });
promiseReturningService
.getUserData()
.then((userData) => {
// this only runs if there is no error getting user data
return userData.userName;
})
.then((userName) => {
// this only runs if there is no error in all previous calls
console.log(userName);
})
@cmstead
cmstead / App.js
Last active January 28, 2019 21:59
Proof of concept: Building a react app using Dject as an IoC container
import logo from './logo.svg';
import './App.css';
function AppFactory(React) {
const { Component } = React;
class App extends Component {
render() {
return (
@cmstead
cmstead / original-code.js
Created January 13, 2019 02:41
Eliminating conditional blocks with value selection and assignment
function async saveDataOrDefault(todoId, messageRecord) {
if(messageRecord !== null) {
await dbModel.saveData(todoId, messageRecord);
} else {
await dbModel.saveData(todoId, { message: null });
}
}
@cmstead
cmstead / dject-test-example.js
Last active April 5, 2018 19:56
Test example using Dject
'use strict';
const assert = require('chai').assert;
const sinon = require('sinon');
const container = require('../example-container');
describe('file reader', function () {
let fileReader;
let logSpy;