Skip to content

Instantly share code, notes, and snippets.

@mir4ef
mir4ef / file-input.component.js
Last active June 30, 2016 11:45
Capture a file from input type='file' with AngularJS
/**
* @file file-input.component.js
* @author Miroslav Georgiev
* @version 0.0.1
*/
(function () {
'use strict';
/**
@mir4ef
mir4ef / calc-row-number.html
Last active June 30, 2016 11:45
Calculate row number when using pagination
<td ng-bind="(($index + 1) + (paginationOptions.currentPage - 1) * paginationOptions.itemsPerPage)"></td>
@mir4ef
mir4ef / disable-anchor-tag.html
Last active June 30, 2016 11:45
Disable anchor tag with either css or javascript
<!-- using CSS -->
<head>
<style>
.my-disabled-link {
cursor: default;
pointer-events: none;
}
</style>
</head>
<body>
@mir4ef
mir4ef / git-remove
Created June 30, 2016 17:59
Remove a file/folder from git repo, but keep local
git rm --cached file-name.txt
git rm -r --cached folder-name
git commit -m "removed file-name and/or folder-name"
git push origin master or branch-name
@mir4ef
mir4ef / loop-multi-d-arrays.js
Created July 5, 2016 14:08
Recursive loop over multi dimensional arrays in JavaScript
// code from
// http://stackoverflow.com/questions/15854425/iterate-over-a-javascript-array-without-using-nested-for-loops/15854485#15854485
function printArray(arr) {
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
printArray(arr[i]);
} else {
console.log(arr[i]);
}
@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;
@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 / 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 / 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 / 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)