Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
smailliwcs / dog.js
Created September 22, 2020 15:24
Object-oriented JavaScript
if (typeof Object.create !== "function") {
Object.create = function (proto) {
function Type() { }
Type.prototype = proto;
return new Type();
};
}
function Animal(name) {
var self = this;
@smailliwcs
smailliwcs / underscore.patch
Created September 22, 2020 15:24
Fixing Underscore.php
@@ -176,7 +176,7 @@ all: Wrap return value
$__ = new self;
if(!is_null($iterator)) $collection = $__->map($collection, $iterator);
$collection = (array) $collection;
- if(count($collection) === 0) return true;
+ if(count($collection) === 0) return self::_wrap(true);
return self::_wrap(is_bool(array_search(false, $collection, false)));
}
@@ -191,8 +191,8 @@ filter: Pass key to iterator
@smailliwcs
smailliwcs / output.txt
Created September 22, 2020 15:24
Underscore.php mixin: sort
Array
(
[0] => 5
[1] => 1
[2] => 3
[3] => 0
[4] => 6
[5] => 8
[6] => 4
[7] => 9
@smailliwcs
smailliwcs / multisort.php
Created September 22, 2020 15:24
Underscore.php mixin: multisort
<?php
__()->mixin([
"reindex" => function ($items, $indices) {
return __($indices)->map(function ($index) use ($items) { return $items[$index]; });
},
"multisort" => function ($items, $keys) {
if (count($items) <= 1) {
return $items;
}
@smailliwcs
smailliwcs / App.config
Created September 22, 2020 15:24
Custom configuration sections
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="presidents1" type="System.Configuration.NameValueSectionHandler" />
<section name="presidents2" type="CustomConfig.PresidentListSection, Custom Config" />
</configSections>
<presidents1>
<add key="1" value="George Washington" />
<add key="2" value="John Adams" />
<add key="3" value="Thomas Jefferson" />
@smailliwcs
smailliwcs / App.config
Created September 22, 2020 15:24
NoSleep
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Distance" value="10" />
<add key="Timeout" value="1:00" />
</appSettings>
</configuration>
@smailliwcs
smailliwcs / proxy.php
Created September 22, 2020 15:24
Simple PHP proxy
<?php
function download($url) {
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true
]);
$data = curl_exec($curl);
curl_close($curl);
return $data;
@smailliwcs
smailliwcs / hg-cvs.sh
Created September 22, 2020 15:24
Mercurial and upstream CVS with MQ
# Set up repo
cvs checkout repo
cd repo
hg init
>> .hgignore echo syntax: glob
>> .hgignore echo CVS/*
>> .hgignore echo .cvsignore
# Consult .cvsignore files
hg addremove
# Verify pending changes
@smailliwcs
smailliwcs / cache.js
Created September 22, 2020 15:24
Knockout extender: cache
ko.extenders.cache1 = function (target) {
target.cached = ko.observable(target.peek());
target.cache = function () {
target.cached(target());
};
return target;
};
var x = ko.observable(1).extend({ cache1: {} });
x(2);
@smailliwcs
smailliwcs / coalesce.js
Created September 22, 2020 15:24
Knockout extender: coalesce
ko.extenders.coalesce = function (target, defaultValue) {
var result = ko.computed({
read: function () {
return target() === undefined ? ko.unwrap(defaultValue) : target();
},
write: function (value) {
target(value);
}
});
result.defaultValue = ko.computed(function () {