Skip to content

Instantly share code, notes, and snippets.

@jiggzson
jiggzson / getBetweenBrackets.js
Last active August 29, 2015 14:08
Gets between two provided brackets
/**
* This method gets between a bracket and returns what's between the brackets only
* after it's been balanced. This can be done with a regex however is microsecond
* optimizations aren't too important then this becomes a more versatile solution which
* can accept a multitude of brackets and braces.
* @param {String} ob The opening bracket or brace
* @param {String} cb The closing bracket or brace
* @param {String} str The string containing the brackets
* @param {Integer} start The starting index from where to start searching
* @example getBetweenBrackets('(',')', 'all_of_this(but((I_want_this)))')
var Fraction = {
convert: function( value, opts ) {
var frac;
if( value === 0 ) {
frac = [ 0, 1];
}
else {
if( value < 1e-6 || value > 1e20) {
var qc = this.quickConversion( Number( value ) );
if( qc[1] <= 1e20 ) {
@jiggzson
jiggzson / scientificToDecimal.js
Last active December 25, 2022 21:03
Converts a javascript number from scientific notation to a decimal string
var scientificToDecimal = function (num) {
var nsign = Math.sign(num);
//remove the sign
num = Math.abs(num);
//if the number is in scientific notation remove it
if (/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
var zero = '0',
parts = String(num).toLowerCase().split('e'), //split into coeff and exponent
e = parts.pop(), //store the exponential part
l = Math.abs(e), //get the number of zeros
@jiggzson
jiggzson / withsteps.js
Created November 8, 2014 21:05
This add-on function logs step using nerdamer
nerdamer.register({
parent: 'Helper',
name: 'withsteps',
visible: false,
build: function() {
var core = this;
var operationLog = [];
//write a function which would perform the log
var log = function(operator, symbol1, symbol2, result, is_first) {
if(!is_first) operationLog.pop();
/**
* This function searches an array and its sub-arrays. A condition function can be used
* to limit the results
* @param {Array} arr
* @param {Function} condfn A function with extra conditions
* @param {String} prop A hashable property to identify the object when found
* @param {Object} c A collector object defined by the function
* @returns {Object} An object containing the found objects
*/
function countFrequency(arr, condfn, prop, c) {
@jiggzson
jiggzson / factors.js
Last active August 29, 2015 14:09
Get all the factors of an integer
function factors(num, unique) {
var l = num, i=1, factors = [],
epsilon = 2.2204460492503130808472633361816E-16;
while(i<l) {
var quotient = num/i;
var whole = Math.floor(quotient);
var remainder = quotient-whole;
if(remainder <= epsilon) {
unique && i === whole ? factors.push(i) : factors.push(i, whole);
l = whole;
(function($) {
$.fn.circularMeter = function() {
var defaults = {
dimension: 25,
'border-width': 25,
color: '#ABABAB',
'text-color': '#000',
'z-index': 99
};
var userOptions = typeof arguments[0] === 'object' ? [].shift.apply(arguments) : defaults;
var toNumberPlace = function(n) {
var number = String(n),
l = number.length,
zeroes = l-1,
places = [];
for(var i=0; i<l; i++) {
var digit = Number(number.charAt(i));
places.push(digit * Math.pow(10, zeroes--));
}
return places;
/**
* Inserts an object into an array at a given index or recursively adds items if an array is given.
* When inserting another array, passing in false for unpackArray will result in the array being inserted
* rather than its items.
* @param {Array} arr - The target array
* @param {Array} item - The item being inserted
* @param {Number} index - Where to place the item
* @param {boolean} unpackArray - Will insert the array instead of adding its items
*/
insertArray = Utils.insertArray = function( arr, item, index, unpackArray ) {
@jiggzson
jiggzson / min_array.js
Created August 17, 2016 22:30
Find smallest number in Javascript array. Much faster than Math.min.apply
function min(arr) {
var l, a, b;
while(true) {
l = arr.length;
if(l < 2) return arr[0];
a = arr.pop();
b = arr[l-2];
if(a < b) {
arr.pop();
arr.push(a);