Skip to content

Instantly share code, notes, and snippets.

@nicolopignatelli
nicolopignatelli / dispatcher.js
Last active August 29, 2015 14:02
Basic javascript event dispatcher
/**
* Usage:
* var callback = function(event) { console.log(event); };
* EventDispatcher.addEventListener('event_type', callback);
* EventDispatcher.dispatch('event_type', eventData);
* EventDispatcher.removeEventListener('event_type', callback);
*/
var EventDispatcher = (function() {
var _callbacks = {};
@nicolopignatelli
nicolopignatelli / keybase.md
Created October 17, 2014 19:01
keybase.md

Keybase proof

I hereby claim:

  • I am nicolopignatelli on github.
  • I am nicolopignatelli (https://keybase.io/nicolopignatelli) on keybase.
  • I have a public key whose fingerprint is A849 FA73 246D 070C E2A6 B1DC AA8D 4CF5 B7B2 06CB

To claim this, I am signing this object:

<?php
function bar(){
echo 'foo';
};
namespace Foo;
function foo(){
echo 'foo';
@nicolopignatelli
nicolopignatelli / gtin_validator.php
Created April 12, 2016 14:20
[PHP] GTIN validator
<?php
function gtinIsValid($gtin) {
$gtin = str_pad(trim($gtin), 14, "0", STR_PAD_LEFT);
$checkSum = 0;
$gtinBody = substr($gtin, 0, strlen($gtin) - 1);
$lastDigit = substr($gtin, -1);
foreach(str_split($gtinBody) as $i => $char) {
if ($i % 2 == 1) {
@nicolopignatelli
nicolopignatelli / functional_service_definition.php
Last active November 27, 2017 22:02
How to implement a service endpoint using a functional style and monadic response objects
<?php
// just a factory method for a Result
function result(callable $f, ...$args): Result {
try {
$value = $f(...$args);
return new Success($value);
} catch(\Exception $e) {
return new Failure(new ExceptionStack($e));
}
package com.mywonderfulbnb.bnb.definition
final class Name {
private String value;
public Name(String value) {
if (value.isEmpty()) {
throw new NameCannotBeEmpty();
}
package com.mywonderfulbnb.bnb.definition
final class BnbId {
private uuid;
public BnbId(UUID uuid) {
this.uuid = uuid;
}
}
package com.mywonderfulbnb.bnb.definition
final class ActivatedOwnerNotFound extends Exception {
private OwnerId ownerId;
public ActivatedOwnerNotFound(OwnerId ownerId) {
this.ownerId = ownerId;
}
}
package com.mywonderfulbnb.bnb.definition
final class Bnb {
private BnbId bnbId;
private OwnerId ownerId;
private Name name;
public Bnb(BnbId bnbId, OwnerId ownerId, Name name) {
this.bnbId = bnbId;
this.ownerId = ownerId;
package com.mywonderfulbnb.bnb.definition
interface Bnbs {
Optional<Bnb> load(BnbId);
void save(Bnb);
}