Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
smailliwcs / dog.js
Created Sep 22, 2020
Object-oriented JavaScript
View dog.js
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 Sep 22, 2020
Fixing Underscore.php
View underscore.patch
@@ -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 Sep 22, 2020
Underscore.php mixin: sort
View output.txt
Array
(
[0] => 5
[1] => 1
[2] => 3
[3] => 0
[4] => 6
[5] => 8
[6] => 4
[7] => 9
@smailliwcs
smailliwcs / multisort.php
Created Sep 22, 2020
Underscore.php mixin: multisort
View multisort.php
<?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 Sep 22, 2020
Custom configuration sections
View App.config
<?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" />
View App.config
<?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 Sep 22, 2020
Simple PHP proxy
View proxy.php
<?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 Sep 22, 2020
Mercurial and upstream CVS with MQ
View hg-cvs.sh
# 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 Sep 22, 2020
Knockout extender: cache
View cache.js
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 Sep 22, 2020
Knockout extender: coalesce
View coalesce.js
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 () {
You can’t perform that action at this time.