Skip to content

Instantly share code, notes, and snippets.

@mir4ef
mir4ef / async-await-handler.js
Created August 23, 2019 15:43
async/await utility function for more elegant handling of failures rather than adding try/catch blocks everywhere. This allows us to also know what exactly failed.
// async/await utility function for more elegant handling of failures
const asyncAwaitHandler = promise => {
return promise
.then(response => ({ error: null, response }))
.catch(error => Promise.resolve({ error, response: null }));
};
// example promise
const myPromise = (errorMe) => {
if (errorMe) {
@mir4ef
mir4ef / async-await-handler.ts
Last active September 21, 2019 04:59
async/await utility function for more elegant handling of failures rather than adding try/catch blocks everywhere. This allows us to also know what exactly failed.
interface IAsyncAwait<T, R> {
error: R|Error|null;
response: T|null;
}
// async/await utility function for more elegant handling of failures
const asyncAwaitHandler: <A, B>(promise: Promise<A>) => Promise<IAsyncAwait<A|null, B|null>> = <A, B>(promise: Promise<A>): Promise<IAsyncAwait<A|null, B|null>> => {
return promise
.then((response: A): IAsyncAwait<A, null> => ({ error: null, response }))
.catch((error: B): Promise<IAsyncAwait<null, B>> => Promise.resolve({ error, response: null }));
@mir4ef
mir4ef / deep-merge.ts
Created January 9, 2018 03:14
Deep merging of JavaScript objects (in TypeScript)
interface IIsObject {
(item: any): boolean;
}
interface IObject {
[key: string]: any;
}
interface IDeepMerge {
(target: IObject, ...sources: Array<IObject>): IObject;
@mir4ef
mir4ef / deep-merge.js
Created January 9, 2018 02:44
Deep Merge JavaScript Objects
/**
* @description Method to check if an item is an object. Date and Function are considered
* an object, so if you need to exclude those, please update the method accordingly.
* @param item - The item that needs to be checked
* @return {Boolean} Whether or not @item is an object
*/
function isObject(item) {
return (item === Object(item) && !Array.isArray(item));
}
@mir4ef
mir4ef / sonar-project.properties
Last active August 7, 2017 20:11
Sonar properties file for angular 2+ applications
sonar.projectKey = company:mycompay
sonar.projectName = My Project
sonar.projectVersion = 0.0.1
sonar.sourceEncoding = UTF-8
sonar.sources = src/app
sonar.exclusions = **/node_modules/**,**/*.spec.ts
sonar.tests = src/app
sonar.test.inclusions = **/*.spec.ts
sonar.ts.tslint.configPath = tslint.json
@mir4ef
mir4ef / flatten-array.js
Last active July 7, 2017 03:24
Flatten a multi dimensional array to 1D array
'use strict';
/**
* @method flattenArray
* @description A method to convert multi dimensional arrays to 1D arrays
* @param {Array} arr - The multi D array that needs to be flattened (converted to 1D)
* @param {String} [key] - An object key that will be used to flatten an array of
* nested objects (e.g. "[ { key: [ val1, val2, ..., valN ] }, { key: [ val1, val2, ..., valN ] } ]")
* @param {Boolean} [remove] - Flag to indicate whether or not to delete object's children if
* they are not need it after flattening (e.g. if true, will remove "key: [ val1, val2, ..., valN ]" after it is being flattened)
@mir4ef
mir4ef / save-blob-ctrl.js
Created November 29, 2016 19:31
Save a blob file in AngularJS 1.x
function SaveFileCtrl (SaveFileService) {
var vm = this;
function downloadFile(someArgument) {
SaveFileService.downloadFile({
param: someArgument
}, function (response) {
var fileName = response.headers['content-disposition'].split("=")[1].replace(/\"/gi,'');
var fileType = response.headers['content-type'] + ';charset=utf-8';
var blob = new Blob([response.data], { type: fileType });
@mir4ef
mir4ef / insert-into-sorted-array.js
Last active November 10, 2023 07:51
insert an element into a sorted array of objects
// 1d array
for (var i = 0, len = arr.length; i < len; i++) {
if (somevalue < arr[i]) {
arr.splice(i, 0, somevalue);
break;
}
}
return arr;
@mir4ef
mir4ef / regexp-date-validation-collection.js
Last active July 15, 2016 19:35
RegExp to validate a date format in JS, but needs extra checks for leap years and 30 day months
// date format: 'YYYY-MM-dd'
var RegExp = /^(199\\d)|([2-9]\\d{3})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/;
// date format: 'MM/dd/YYYY',
RegExp = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
// passed as a string
var regexp = new RegExp('^(0[1-9]|1[0-2])\/(0[1-9]|1\\d|2\\d|3[01])\/(19|20)\\d{2}$');
@mir4ef
mir4ef / object-sorting-2.js
Last active June 7, 2017 13:23
Sorting an object with mixed content (numbers && letters)
// from https://www.sitepoint.com/sort-an-array-of-objects-in-javascript/
function compareValues(key, order='asc') {
return function(a, b) {
if(!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
return 0;
}
const valA = (typeof a[key] === 'string') ? a[key].toUpperCase() : a[key];
const valB = (typeof b[key] === 'string') ? b[key].toUpperCase() : b[key];
let comparison = 0;