Skip to content

Instantly share code, notes, and snippets.

View akhoury's full-sized avatar
🏠
Working from home

Aziz Khoury akhoury

🏠
Working from home
View GitHub Profile
/*
example:
// [1,2,3,4,5,6,7,8] (must be a power of 2 length)
var arr = new Foldable([1, 2, 3, 4, 5, 6, 7, 8]);
arr.fold( [ "l", "r", "l" ] ); // left, right, left
// left
@akhoury
akhoury / augment.js
Last active December 21, 2015 14:48
augment a function
var augment = function (base, extra, context) {
return (function () {
return function () {
base.apply(context || this, arguments);
extra.apply(context || this, arguments);
};
})();
};
// usage
@akhoury
akhoury / augment.coffee
Created August 23, 2013 17:29
augment.coffee
augment = (base, extra, context) ->
(->
->
base.apply context or this, arguments_
extra.apply context or this, arguments_
)()
@akhoury
akhoury / handlebar-css-js-inject.handlebars
Last active December 22, 2015 11:38
Inject CSS and JS tag in an handlebar template
<script>
// document ready, using jquery
$(function() {
var injectScript = function(src, options) {
options || (options = {});
injectTag('script', {src: src, type: 'text/javascript'}, options);
};
@akhoury
akhoury / nodebb.emails.js
Last active January 2, 2016 11:09
get your nodebb users emails in a CSV file to import to mailchimp or something
// !!!!! IMPORTANT !!!!!!
// this is no longer needed since a helper users/csv generator was added by @psychobunny
// commit: https://github.com/designcreateplay/NodeBB/commit/cfa4256df5c2e7d9f0c1969a8098ef8472d2fa93
// in order for this to work
// - this JS file needs to be located in: [NodeBB_PATH]/node_modules/_nodebb_get_users_emails
// - you must have a working NodeBB installation, with config.json and a running database
// USE IT THEN DELETE IT
@akhoury
akhoury / prePRpreview.js
Last active January 3, 2016 22:29
- PreNodeBB-PullRequest preview: - Affected files: public/src/utils.js, src/meta.js - Purpose: Allowing the 'config' hash to safely save Stringified Objects and Arrays, but leave everything else alone.
// var definitions for demo-gist only
// there is also a mock util object below
var Meta = {};
var Meta.configs = {
// .. other Meta.configs functions, such as set, get, getFields etc...
// obviously the function names aren't the best, but I didn't want to override existing ones
// sets a config field in the DB, as a stringified JSON if it's an Object or an Array,
@akhoury
akhoury / handlebars-helper-x.js
Last active February 17, 2024 13:25
Handlebars random JavaScript expression execution, with an IF helper with whatever logical operands and whatever arguments, and few more goodies.
// for detailed comments and demo, see my SO answer here http://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional/21915381#21915381
/* a helper to execute an IF statement with any expression
USAGE:
-- Yes you NEED to properly escape the string literals, or just alternate single and double quotes
-- to access any global function or property you should use window.functionName() instead of just functionName()
-- this example assumes you passed this context to your handlebars template( {name: 'Sam', age: '20' } ), notice age is a string, just for so I can demo parseInt later
<p>
{{#xif " name == 'Sam' && age === '12' " }}
BOOM
@akhoury
akhoury / npm-mod-install-fail.js
Last active August 29, 2015 14:05
node v0.10.3, npm v1.4.4 A test script to install module via npm, that when attempting to uninstall a module (which was installed from the npm registry) then installing the same module from a URL (github.com), the 2nd call to require attempts to execute the old main script which does not execute, even after clearing require's cache.
var fs = require('fs');
var getModuleShortName = function(giturlORname) {
if (giturlORname.indexOf('git://github.com') > -1) {
return giturlORname.split('/').pop().split('#')[0];
}
return giturlORname.split('@')[0];
};
var searchModulesCache = function(moduleName, callback) {
var npm = require('npm');
var fs = require('fs');
var path = require('path');
module.constructor.new_packageMainCache = {}; // own cache
// copied from node src
function statPath(path) {
try {
var isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
var getStorage = function(key) {
var data = localStorage.getItem(key),
ttl = localStorage.getItem(key + '-ttl'),
expired = ttl == null ? false : ttl < (new Date()).getTime();