Skip to content

Instantly share code, notes, and snippets.

View kmaida's full-sized avatar

Kim Maida kmaida

View GitHub Profile
@kmaida
kmaida / dateAndTimeStringsToDate.js
Last active May 22, 2017 19:14
Date (mm/dd/yyyy) and time (hh:mm AM/PM) strings to JS Date
// MM/DD/YYYY, M/D/YYYY
const dateRegex = new RegExp(/^(\d{2}|\d{1})\/(\d{2}|\d{1})\/\d{4}$/);
// HH:MM am/pm, HH:MM AM/PM
const timeRegex = new RegExp(/\b((1[0-2]|0?[1-9]):([0-5][0-9]) ([AaPp][Mm]))/);
// Convert date and time strings to a Date object
function stringsToDate(dateStr, timeStr) {
if (!dateRegex.test(dateStr) || !timeRegex.test(timeStr)) {
return null;
}
@kmaida
kmaida / capitalize.js
Created May 15, 2017 18:03
Capitalize string with JS
function capitalize(str) {
// Capitalize first letter of string
return str.charAt(0).toUpperCase() + str.slice(1);
}
@kmaida
kmaida / formUtils.factory.ts
Last active August 21, 2020 14:06
Angular Date Validator - directive and factory - validates strings m/d/yyyy
// MM/DD/YYYY, M/D/YYYY
const DATE_REGEX = new RegExp(/^(\d{2}|\d{1})\/(\d{2}|\d{1})\/\d{4}$/);
export { DATE_REGEX };
@kmaida
kmaida / slugify.js
Last active July 5, 2020 19:32
Convert string of text into a slug separated by hyphens with no special characters.
function slugify(text) {
const a = 'ãàáäâèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;';
const b = 'aaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------';
const p = new RegExp(a.split('').join('|'), 'g');
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special chars
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
@kmaida
kmaida / randomDates.js
Last active April 26, 2017 15:19
Generate random dates within a range
/**
* Generate a random date between now and one year from now
* With a time between 7am and 8pm
* Minutes on the full hour :00
* @returns {Date}
*/
function randomDate() {
var start = new Date();
var end = new Date(new Date().setFullYear(start.getFullYear() + 1));
var date = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
@kmaida
kmaida / ordinal.js
Created April 24, 2015 14:46
Get an ordinal from a number
/**
* Get ordinal value
*
* @param n {number} if a string is provided, % will attempt to convert to number
* @returns {string} th, st, nd, rd
*/
function getOrdinal(n) {
var ordArr = ['th', 'st', 'nd', 'rd'];
var modulus = n % 100;
@kmaida
kmaida / _mq.scss
Created April 3, 2015 16:28
Sass media query mixin that accepts media query string as argument
/*-- Media query variables --*/
$mq-small: 'media and (max-width: 767px)';
$mq-large: 'media and (min-width: 768px)';
/*-- Media query mixin --*/
@mixin mq($mqString) {
@media #{$mqString} {
@content;
@kmaida
kmaida / scrollPosition.js
Last active August 29, 2015 14:16
Get the current position of the scrollbar with JavaScript
// Firefox uses HTML, Webkit uses body, iOS uses pageYOffset, therefore check for any:
var scrollPosition = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset;
@kmaida
kmaida / noWidowsFilter.js
Created February 23, 2015 03:44
AngularJS - filter to fix widows in content that has been bound as HTML (will not work inside {{ }}). Usage: ng-bind-html="content | noWidows | trustAsHTML"
myApp.filter('noWidows',function(){
return function(input){
var wordArray = input.split(' ');
if (wordArray.length > 1) {
wordArray[wordArray.length - 2] += ' ' + wordArray[wordArray.length - 1];
wordArray.pop();
return wordArray.join(' ');
} else {
@kmaida
kmaida / sortByValue.js
Created February 6, 2015 21:34
AngularJS - sort by value
$scope.orderByValue = function(value) {
return value;
}
// usage: ng-repeat="item in items | orderBy:orderByValue"