Skip to content

Instantly share code, notes, and snippets.

View miguel-leon's full-sized avatar

Miguel Leon miguel-leon

View GitHub Profile
@miguel-leon
miguel-leon / angular1.x-crypto.js
Created June 19, 2017 01:10
Angular 1.x service for Web Cryptographic API
angular.module('crypto', [])
.provider('crypto', function () {
var crypto = window.crypto, subtle;
if (!crypto || !(subtle = crypto.subtle || crypto.webkitSubtle)) {
throw new Error('Web Cryptographic API not supported');
}
var hashAlg = {name: 'SHA-1'};
var keyDerivationAlg = {name: 'PBKDF2', iterations: 500000, hash: hashAlg};
@miguel-leon
miguel-leon / enum-map.ts
Created September 9, 2017 23:03
Enum mapper for Typescript
export class EnumMap<E> {
values: string[];
enum_: E;
private constructor(enum_: E, mappings: EnumMap.Type<E>) {
this.values = Object.keys(mappings);
this.enum_ = enum_;
return Object.assign(Object.create(this), mappings);
}
@miguel-leon
miguel-leon / splitWords.ts
Created September 23, 2018 03:45
Split words in a camel case string
function splitWords(str: string) {
return str.replace(/([a-z]|[A-Z]+)([A-Z]+$|[A-Z])/g, "$1 $2");
}
@miguel-leon
miguel-leon / 1-EnumMap.ts
Last active October 9, 2018 17:40
Mappings for enums in typescript
export class EnumMap<E, T> {
values: string[];
enum_: E;
private constructor(enum_: E, mappings: EnumMap.Type<E, T>) {
this.values = Object.keys(mappings);
this.enum_ = enum_;
return Object.assign(Object.create(this), mappings);
}
@miguel-leon
miguel-leon / Action.ts
Last active October 16, 2018 22:34
Auto typed actions to use with stores
export interface Action {
readonly type: string;
}
@miguel-leon
miguel-leon / appendHtml.js
Created November 20, 2018 22:37
parse html and append to element
function appendHtml(element, content) {
const aux = document.createElement(element.tagName);
aux.innerHTML = content;
for (let i = 0; i < aux.children.length; i++) {
element.appendChild(aux.children[i]);
}
}
export function* LazySequence(to, from = 0) {
while (from < to) {
yield from++;
}
}
export {};
declare global {
interface ArrayConstructor {
range(size, start?): number[];
}
}
Array.range = Array.range || function (size, start = 0) {
return Array.from(Array(size + start).keys()).slice(start);
@miguel-leon
miguel-leon / idx.js
Created June 12, 2019 19:16
safe access deep properties
const idx = (o, ...p) => p.reduce((a, i) => a && a[i], o)
@miguel-leon
miguel-leon / deepSet.ts
Created August 14, 2019 16:21
safely deep set nested object properties
function deepSet(value: any, obj: object, ...path: string[]) {
const target = path.slice(0, -1).reduce((p: any, k) => (p[k] = p[k] || {}), obj);
path.slice(-1).forEach(k => target[k] = value);
}
let o;
deepSet(99, o = { a: { x: 88 } }, ...'a.b.c'.split('.'));
console.log(o);