Skip to content

Instantly share code, notes, and snippets.

@teckliew
teckliew / ExportSVGLayers.jsx
Created August 14, 2019 05:46
Adobe Illustrator script that exports SVG for every layer in the document.
/**********************************************************
ExportSVGLayers.jsx
DESCRIPTION
For Adobe Illustrator.
This script takes your active document and export one
SVG per layer.
Best used for exporting many layers with the same artboard
@teckliew
teckliew / protips.js
Created October 19, 2015 04:36 — forked from nolanlawson/protips.js
Promise protips - stuff I wish I had known when I started with Promises
// Promise.all is good for executing many promises at once
Promise.all([
promise1,
promise2
]);
// Promise.resolve is good for wrapping synchronous code
Promise.resolve().then(function () {
if (somethingIsNotRight()) {
throw new Error("I will be rejected asynchronously!");
@teckliew
teckliew / if.js
Last active October 8, 2015 20:01
alternatives to if's. if statement refactored.
//if statement
(if ( foo ) {
bar = "beta";
}
// and...
if ( foo ) {
bar();
})
//alternative
@teckliew
teckliew / shuffle.js
Last active October 6, 2015 22:45
Non-bias random array shuffle. For more about the Fisher–Yates shuffle, see the Wikipedia article and Jeff Atwood’s post, “The Danger of Naïveté” (2007). Sauce: http://bost.ocks.org/mike/shuffle/
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
@teckliew
teckliew / 0_reuse_code.js
Last active August 29, 2015 14:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@teckliew
teckliew / ajax.js
Last active August 29, 2015 14:23
AJAX function with without jQuery. JS only.
var ajax = {
load : function() {
var xhr;
if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
else {
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
@teckliew
teckliew / toCurrency.js
Created June 17, 2015 20:00
a function that takes an integer in input and outputs a string with currency format. Ex. 123456 -> "123,456"
//my solution using REGEX
function toCurrency(price){
return price.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}
//good example
function toCurrency(price){
return price.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
}
@teckliew
teckliew / descendingOrder.js
Created June 17, 2015 19:58
Make a function that can take any non-negative integer as a argument and return it with it's digits in descending order.
//my example
function descendingOrder(n){
var digits = (""+n).split("").sort(function(a, b){return b-a});
digits = digits.join("");
return Number(digits);
}
//good examples
//1
function descendingOrder(n){
@teckliew
teckliew / XtoYofZ.js
Created June 17, 2015 19:54
Showing X to Y of Z Products. Given the page number, page size, and the total number of product.
//my solution
var paginationText = function(pageNumber, pageSize, totalProducts){
var numOfPages = Math.ceil(totalProducts/pageSize);
var initItem = pageNumber*pageSize-(pageSize-1);
var endItem;
if (pageNumber === numOfPages){
endItem = totalProducts;
}else{
endItem = pageSize*pageNumber;
};
@teckliew
teckliew / sumOfAllNum.js
Created June 17, 2015 19:51
The js solution to sum of all numbers from 1 to n
//my solution
function f(n){
return n > 0 && n % 1 === 0 ? ((1+n)*n)/2 : false;
};
//good examples
//1
function f(n){
return (parseInt(n) === n && n > 0) ? n*(n+1)/2 : false;
};