Skip to content

Instantly share code, notes, and snippets.

View gladchinda's full-sized avatar

Glad Chinda gladchinda

View GitHub Profile
@remy
remy / round.js
Created February 24, 2018 17:29
Better/more accurate Math.round
const round(value, decimals) =>Number(Math.round(value+'e'+decimals)+'e-'+decimals);
round(1.005, 2); // 1.01
Math.round(1.005, 2); // 1.00
// via http://www.jacklmoore.com/notes/rounding-in-javascript/
@bithavoc
bithavoc / nodejs_asymm_crypto_sample.js
Created October 20, 2011 02:17
Asymmetric Encryption Sample in Node.js: Encrypt and Decrypt using Password as a key(SECRET_KEY) / Triple-DES
/*
Asymmetric Encryption Sample in Node.js: Encrypt and Decrypt using Password as a key(SECRET_KEY)
Algorithm: des-ede3-cbc (Three key Triple-DES EDE in CBC mode)
johan@firebase.co
@thepumpkin
*/
var assert = require('assert')
var crypto = require('crypto')
var Buffer = require('buffer').Buffer
/*
* How to detect which element is the scrolling element in charge of scrolling the viewport:
*
* - in Quirks mode the scrolling element is the "body"
* - in Standard mode the scrolling element is the "documentElement"
*
* webkit based browsers always use the "body" element, disrespectful of the specifications:
*
* http://dev.w3.org/csswg/cssom-view/#dom-element-scrolltop
*
function getValueFromPath(object, path) {
const OBJECT_TYPE = '[object Object]';
const $type = Function.prototype.call.bind(Object.prototype.toString);
// Ensure that path is a string, default to an empty string if not provided.
// Replace bracket notation occurrences on path with dot notation.
// Split path (to array) using the `.` delimeter, discard empty elements.
path = (path ? String(path) : String())
.replace(/\[((?:['"])?)([^[\]]*)\1\]/g, function __replacer__($, $$, key) {
return `.${Number(key) || String(key)}`;
@josemcunha
josemcunha / README-Template.md
Last active December 19, 2020 11:06 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here. Don't forget to link back to the tutorial.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@remy
remy / next.config.js
Created July 18, 2017 18:37
Next.js configuration for dotenv and custom servers.
const webpack = require('webpack');
require('dotenv').config({
path: process.env.NODE_ENV === 'production' ? '.env.production' : '.env'
});
module.exports = {
webpack: config => {
const env = Object.keys(process.env).reduce((acc, curr) => {
acc[`process.env.${curr}`] = JSON.stringify(process.env[curr]);

JS Data Structures — Maps and Sets

The way, in which data is structured, plays a vital role in being able to efficiently perform certain operations on the data or solve certain problems in relation to the data. For example you can delete any item from a doubly linked list in constant time, whereas that could take linear time if the list is represented as an array. Also, searching for the presence of a key in an array of keys can be done more efficiently (in logarithmic time) when the array is sorted than when not sorted.

Some very popular programming languages like Java and Python provide lots of useful data structure implementations out of the box, as part of their standard library; whereas the ubiquitous "JavaScript" programming language appears to be pretty lean in that regard. However, like most programming languages, JavaScript ships with some very basic data types — such as arrays, strings, objects, sets, maps, etc.

Keyed Collections

Prior to the ECMAScript 2015 specification updates (_

@glenjamin
glenjamin / pool-transaction.js
Last active October 22, 2021 00:46
DB transaction from a connection pool in node-mysql
var mysql = require('mysql');
var pool = mysql.createPool('mysql://localhost');
inTransaction(pool, function(db, next) {
db.query("DELETE * FROM stuff", function(err) {
if (err) return next(err);
db.query("INSERT INTO stuff VALUES (1,2,3)", function(err) {
return next(err);

finally-polyfill

A tiny ~150-byte polyfill for Promise.prototype.finally.

Useful for browsers that support Promise but not the .finally() method.

Usage

npm install finally-polyfill

@sspencer
sspencer / transparent-gif.js
Created October 31, 2010 22:27
Serve a transparent GIF from NodeJS
// Two ways to serve transparent GIF
var buf = new Buffer([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00,
0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b]);
res.send(buf, { 'Content-Type': 'image/gif' }, 200);