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 / 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 / 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 / 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 / angular1.5-configurator.js
Created June 19, 2017 00:41
Routes configurator for angular 1.5
angular.module('configurators', ['ngRoute', 'ngMaterial'])
.config(['$provide', '$routeProvider', 'Resolver', function ($provide, $routeProvider, Resolver) {
var modules = {};
$provide.constant('RouteConfigurationProvider', function (module, options) {
if (arguments.length === 1) {
if (angular.isObject(module)) {
options = module;
@miguel-leon
miguel-leon / Array.prototype.groupBy.js
Last active June 19, 2017 00:35
group by for arrays
/**
* @method Array.prototype.groupBy(group)
* Creates a dictionary grouping by an attribute or if `group` is a function, what it returns.
* @param {function|string} group - attribute name or function(item, index, array): string
* @returns {object.<array>}
*/
if (!Array.prototype.groupBy) {
Object.defineProperty(Array.prototype, 'groupBy', {
enumerable: false,
value: function groupBy(group) {
@miguel-leon
miguel-leon / util-functions.js
Last active March 29, 2017 21:31
Few JS functions
/**
* Extends `obj` with properties created as `key: value` for every pair of two consecutive arguments
* @param {Object} obj
* @return {Object} obj
*/
function extendWithArgs(obj) {
for (var i = 2; i < arguments.length; i += 2) {
obj[arguments[i-1]] = arguments[i];
}
return obj;
@miguel-leon
miguel-leon / services-roles.js
Created February 5, 2017 06:14
Generalization of some "roles" for services regarding rest resources to allow reuse of code.
angular.module('services-roles', [])
.factory('HelperRole_CommonResourceGetter', function (params, errorHandler) {
/**
* @constructor
* @param {string} name of the resource
* @param {Resource} Resource
* @param {Function.<Array|boolean>} actionArgsMapper
*/
@miguel-leon
miguel-leon / ng-attribute-constructor.js
Created February 5, 2017 06:07
Sort of decorator so that the Attribute class constructor function is obtainable
angular.module('ng-attribute', [])
// Horrible hack for obtaining directive class `Attribute` constructor function.
// There is a nuisance in AngularJS in that the `constructor` property was not retained in the prototype.
.directive('attributesDecorator', function () {
// constructor extracted from angular.js
function Attributes(element, attributesToCopy) {
if (attributesToCopy) {
var keys = Object.keys(attributesToCopy);
var i, l, key;
@miguel-leon
miguel-leon / template-cache-watch.js
Created February 5, 2017 05:59
angular.js decorator for $templateCache to add callbacks for when templates become registered
angular.module('$template-cache-watch', [])
.decorator('$templateCache', function ($delegate) {
var decorated = Object.create($delegate);
var callbacks = Object.create(null);
decorated.watch = function (key, callback) {
if (!callbacks[key]) {
callbacks[key] = [];
@miguel-leon
miguel-leon / Object.fromSchema.js
Created December 20, 2016 19:17
Create object from schema
if (!Object.fromSchema) {
Object.defineProperty(Object, 'fromSchema', {
enumerable: false,
value: function fromSchema(schema) {
var result = {};
Object.keys(schema).forEach(function (key) {
value = schema[key];
if (value && value.constructor === Function) {
result[key] = value();
}