Skip to content

Instantly share code, notes, and snippets.

View kirpalmakanga's full-sized avatar

Kirpal Makanga kirpalmakanga

View GitHub Profile
const getUrlParams = (url) => {
const { searchParams } = new URL(url);
return Object.fromEntries([...searchParams.entries()]);
};
@kirpalmakanga
kirpalmakanga / create-reducer.js
Created May 10, 2019 08:41
Little helper for reducer generation
const createReducer = (initialState = {}, handlers = {}) => (
state = {},
action = {}
) =>
handlers.hasOwnProperty(action.type)
? handlers[action.type](state, action)
: { ...initialState, ...state };
export default createReducer;
@kirpalmakanga
kirpalmakanga / google-auth.ts
Last active May 28, 2025 15:07
Loading Google API and auth service
const API_URL = 'https://apis.google.com/js/api.js';
const GOOGLE_CLIENT_ID = '';
const GOOGLE_CLIENT_SCOPE = '';
function loadScript(src) {
return new Promise((resolve, reject) => {
try {
if (!document.querySelector(`script[src="${src}"]`)) {
@kirpalmakanga
kirpalmakanga / csv.js
Last active March 12, 2019 11:42
Client-side CSV export
function processRow(row, separator) {
let finalVal = "";
for (let j = 0; j < row.length; j++) {
let innerValue = row[j] === null ? "" : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
}
@kirpalmakanga
kirpalmakanga / contains-html.js
Created October 10, 2018 08:54
Check if string contains html tags
export const containsHTML = (str) => /<[a-z][\s\S]*>/i.test(str);
@kirpalmakanga
kirpalmakanga / basicToken.js
Last active August 23, 2018 13:35
Generate basic token
import { Buffer } from 'buffer';
const generateBasicToken = (user, pass) =>
new Buffer([user, pass].join(':')).toString('base64');
@kirpalmakanga
kirpalmakanga / compose.js
Last active July 18, 2019 20:04
Async Compose & Pipe
const asyncCompose = (…functions) => input => functions.reduceRight((chain, func) => chain.then(func), Promise.resolve(input));
const asyncPipe = (…functions) => input => functions.reduce((chain, func) => chain.then(func), Promise.resolve(input));
@kirpalmakanga
kirpalmakanga / hexToRGBA.js
Last active October 19, 2018 12:55
hex to RGBA
const parseHex = (hex) => (startChar, endChar) =>
parseInt(hex.slice(startChar, endChar), 16);
function hexToRGBA(hex, a = 1) {
const r = parseHex(hex)(1, 3);
const g = parseHex(hex)(3, 5);
const b = parseHex(hex)(5, 7);
return `rgba(${r},${g},${b},${a})`;
}
@kirpalmakanga
kirpalmakanga / pipe.js
Last active August 22, 2018 08:16
Pipe function (sync)
const pipe = (...ops) => ops.reduce(
(a, b) => (...arg) => b(a(...arg))
)
@kirpalmakanga
kirpalmakanga / jsonSize.js
Last active January 18, 2018 14:19
Get json string size
const bytes = (s) => ~-encodeURI(s).split(/%..|./).length;
const jsonSize = (s) => bytes(JSON.stringify(s));