Skip to content

Instantly share code, notes, and snippets.

View trafficinc's full-sized avatar

Ron trafficinc

  • Greater Philadelphia
View GitHub Profile
@trafficinc
trafficinc / JSuuid.js
Created November 13, 2019 18:34
UUID in JS
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, function(c) {
return (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
});
}
console.log(uuidv4());
/* will produce.
"f89edcd5-0667-48eb-8939-23297025d7f3"
@trafficinc
trafficinc / getDiff.js
Created November 11, 2019 14:07
Get Difference between Two Strings
function getDiff(a, b) {
var i = 0;
var j = 0;
var result = "";
while (j < b.length) {
if (a[i] != b[j] || i == a.length) {
result += b[j];
} else {
i++;
@trafficinc
trafficinc / stringtoInteger.js
Created November 11, 2019 14:03
Converts a String to a 32bit integer
String.prototype.hashCode = function () {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
@trafficinc
trafficinc / stristr.js
Created November 7, 2019 20:30
JavaScript, finds first occurrence of a string within another, case insensitive. Inspiration from the similar PHP function.
function stristr (haystack, needle, bool) {
var pos = 0;
haystack += '';
pos = haystack.toLowerCase().indexOf((needle + '').toLowerCase());
if (pos == -1) {
return false;
} else {
if (bool) {
return haystack.substr(0, pos);
@trafficinc
trafficinc / eventBus.js
Last active November 13, 2019 18:40
Event Bus Module
var EventBus = (function(){
"use strict";
var subscriptions = {};
var getNextUniqueId = getIdGenerator();
var Bus = {
subscribe: function(eventType, callback) {
var id = getNextUniqueId();
@trafficinc
trafficinc / addressComp.js
Created March 14, 2019 13:59
Address Comp JS
// specs code :: Jasmine
describe("Address Tests", function() {
it("Full Address", function() {
var appResponse = {
user: {
address: "120 Oak Avenue",
city: "Philadelphia"
}
};
@trafficinc
trafficinc / wordPatternAlgo.js
Created March 14, 2019 13:47
Word Pattern Algo
var wordPattern = function(pattern, str) {
var letters = pattern.split('');
var symbols = {};
var reverseSymbols = {};
var words = str.split(' ');
if (letters.length != words.length) return false;
for (var i = 0; i < letters.length; ++i) {
var word = words[i];
@trafficinc
trafficinc / menumake.php
Created August 4, 2018 01:27
Create menu from array
<?php
$data = array(
array(
'id' => 6,
'parent_id' => 0,
'depth' => 0,
'left' => 1,
'right' => 2,
'title' => 'Home',
@trafficinc
trafficinc / simpletemplating.js
Created May 20, 2018 23:42
Simple JS Templating w/o framework
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Template</title>
</head>
<body>
<div class="current-price"></div>
@trafficinc
trafficinc / dictionary.js
Last active July 9, 2020 15:58
JavaScript Dictionary OL
myDictionary = {
datastore : [],
add: function(key, value) {
this.datastore.push({key: key, value: value});
},
find: function(key) {
for(var x = 0; x < this.datastore.length; x++) {
if (this.datastore[x] !== undefined && this.datastore[x].key === key) {
return this.datastore[x].value;
}