Skip to content

Instantly share code, notes, and snippets.

View unscriptable's full-sized avatar
🏔️

John Hann unscriptable

🏔️
View GitHub Profile
@unscriptable
unscriptable / AMD-factory-with-require.js
Created November 20, 2012 15:11
Simple AMD/node boilerplate. These aren't quite "normal" AMD and aren't "pure" CJS, but work in AMD loaders and node-like environments (like RingoJS).
// Typical AMD factory that returns a value, but uses an r-value (sync) require(),
// rather than a long, awkward dependency list.
// You cannot use module.exports or exports to declare the module:
(function (define){
define(function (require) {
"use strict";
var mod = require('pkb/modA');
return {
@unscriptable
unscriptable / _fast-curl-boot.md
Created November 29, 2012 04:52
fast ways to boot apps with curl

There are a couple of things that bug me about RequireJS's data-main method of single-script loading:

<script src="js/requirejs/require.js" data-main="app/main.js"></script>
  1. the built file (bundle) must be named "require.js". WAT.
  2. it just seems backwards.
  3. data-main does not follow w3c recommendations since it's not name-spaced.
@unscriptable
unscriptable / frak.js
Last active February 12, 2024 00:38
frak.js
(function (define, frakkedConstructor) { define(function (require) { "use strict";
var removeCommentsRx, findFuncRx, fracPrefixSrc, fracSuffixSrc,
undef;
removeCommentsRx = /\/\*[\s\S]*?\*\/|(?:[^\\])\/\/.*?[\n\r]/g;
findFuncRx = /(function\s+NAME\s*\([^\{]+\{)|(?:[^\\]?)(["'])|(\{)|(\})/g;
// TODO: allow individual parameters to be modified
// TODO: allow function return to be modified or passed to after()
fracPrefixSrc = 'frak$backs.before.apply(this, arguments); try {';
@unscriptable
unscriptable / run-module.js
Last active February 12, 2024 00:38
run.js versus main module
/**
* The bootstrap for the app is a module. The weird thing about this option
* is that it uses curl(), rather than require(), inside the module to load
* other modules, such as the app's main module.
* The script element to execute this bootstrap would look like this:
*
* <script data-main="client/run-module" src="client/lib/curl.js"></script>
*/
define(['curl'], function (curl) {
@unscriptable
unscriptable / hide-replies-bkmklet.js
Last active February 12, 2024 00:38
bookmarklet to collapse all replies and retweets on a person's twitter timeline feed page
javascript:/**(c)2013_unscriptable.com.License:MIT*/(function(){var a=document;try{var b;if(!a.getElementById("page-container"))throw"Are you sure you're on a Twitter page?";b=a.getElementById("collapse-stuff");b||(b=a.createElement("style"),b.id="collapse-stuff",a.head.appendChild(b));0==b.sheet.cssRules.length?b.sheet.insertRule("[data-mentions],[data-retweet-id]{display:none}",0):b.sheet.deleteRule(0)}catch(c){alert("Oops! "+c+" That didn't work :(")}}());
@unscriptable
unscriptable / curl+plain-js.md
Last active February 12, 2024 00:37
should curl.js support non-modules natively?

Q: should curl.js support non-modules natively?

from original Pull Request

Proposal:

code is here

  • Auto-define a module if dev includes a ".js" extension (inject a noop factory: function () {})
  • Fail if the dev used a ".js" extension with modules (define()s found in file)
@unscriptable
unscriptable / main_spec.js
Last active February 12, 2024 00:37
todomvc with an additional "properties view"
define({
// Cujo uses OOCSS principles and thus separates theme (skin)
// from structure CSS.
theme: { module: 'css!theme/base.css' },
// The root node where all the views will be inserted
root: { $ref: 'dom!todoapp' },
// Render and insert the create view
@unscriptable
unscriptable / AMD Modules.js
Last active February 11, 2024 22:26
AMD modules that look like CJS Modules/1.1
define(function (require, exports) {
var foo = require('foo');
exports.bar = 'bar';
});
// this also works:
define(function (require) {
var foo = require('foo');
return { bar: 'bar' };
});
var dfd, p1;
dfd = defer();
p1 = dfd.promise;
p1.then(null, function () { console.log('onRejected 1 failed'); });
p1.then(null, function () { console.log('onRejected 2 failed'); });
dfd.reject('wat');
@unscriptable
unscriptable / isolate.md
Last active February 12, 2024 00:37
example of possible curl/tdd/isolate

curl/tdd/runner is a bit complicated atm. Just thinking of something that might be a bit simpler:

curl(['curl/tdd/isolate'], function (isolate) {

	// inject AMD `require` and `define`, as well as a "done" callback.
	// the test function is guaranteed to run in isolation and all modules 
	// are undefined afterward.
	isolate(function test (require, define, done) {