Skip to content

Instantly share code, notes, and snippets.

View xbill82's full-sized avatar

Luca Marchesini xbill82

  • Kaliop
  • Montpellier, France
View GitHub Profile
@xbill82
xbill82 / fuzzy-search-string
Created November 27, 2013 17:48
JavaScript: Fuzzy search over string
String.prototype.fuzzy = function (s) {
var hay = this.toLowerCase(), i = 0, n = 0, l;
s = s.toLowerCase();
for (; l = s[i++] ;) if ((n = hay.indexOf(l, n)) === -1) return false;
return true;
};
@xbill82
xbill82 / toURLFriendly.php
Created November 28, 2013 08:51
PHP: String to URL friendly
<?
/**
* @param String $str The input string
* @return String The string without accents
*/
function removeAccents( $str )
{
$a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð',
'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã',
'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ',
@xbill82
xbill82 / JsonDecode.robust.php
Created November 28, 2013 08:54
PHP: Robust JSON-decode
<?
/**
* Custom version of the json_decode native function to allow comments in
* the config file.
*/
function json_clean_decode($json, $assoc = false, $depth = 512, $options = 0) {
// search and remove comments like /* */ and //
$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
if(version_compare(phpversion(), '5.4.0', '>=')) {
@xbill82
xbill82 / bg-alpha.less
Last active August 29, 2015 13:56
LESS background alpha Mixin
// --- Cross-browser transparent background trick thanks to
// http://hammerspace.co.uk/2011/10/cross-browser-alpha-transparent-background-css
.bg-alpha(@color, @opacity) {
@alphaColour: hsla(hue(@color), saturation(@color), lightness(@color), @opacity);
@ieAlphaColour: argb(@alphaColour);
background:@color;
background: transparent\9;
background: rgba(red(@color), green(@color), blue(@color), @opacity);
filter: e(%("progid:DXImageTransform.Microsoft.gradient(startColorstr='%d',endColorstr='%d')", @ieAlphaColour, @ieAlphaColour));
zoom: 1
@xbill82
xbill82 / veryFuzzyMatch.php
Last active August 29, 2015 13:56
Very fuzzy word-by-word match
/**
* Matches a string over the other in a fuzzy word-by-word fashion.
* The needle matches the haystack if the haystack contains all the words
* of the needle in any order.
*
* @param String $haystack The string to search in.
* @param String $needle The string that has to be searched for in the
* haystack.
* @return Boolean true if the needle matches the haystack, false
* otherwise.
@xbill82
xbill82 / CharLimit.js
Last active August 29, 2015 13:56
JS Text-field character limiter (jQuery plugin)
(function($) {
$.fn.extend( {
limiter: function(limit, elem) {
$(this).on("keyup focus", function() {
setCount(this, elem);
});
function setCount(src, elem) {
var chars = src.value.length;
if (chars > limit) {
src.value = src.value.substr(0, limit);
@xbill82
xbill82 / custom.map.js
Last active August 29, 2015 14:17
Intercepting dependency with RequireJS
define({
map:{
'*': { 'nav': 'custom/header/nav.custom' }
}
});
@xbill82
xbill82 / Gruntfile.js
Last active August 29, 2015 14:17
Component-specific Styling (and theming) with LESS and Gruntjs
build: {
options: {
paths: [ // Where to look for files to @import
"app/styles/"
]
},
files: [
// @see http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
{
expand: true, // Enable dynamic expansion.
@xbill82
xbill82 / m_article.js
Created March 27, 2015 13:36
Home-made One-to-many relationship in Backbonejs
define(['require', 'backbone', 'collections/c_tags'], function(require) {
"use strict";
var Backbone = require('backbone');
var TagsCollection = require('collections/c_tags');
return Backbone.Model.extend({
tagsCollection: null,
url: function() {
@xbill82
xbill82 / v_btn.js
Last active August 29, 2015 14:17
Simple eventful interface development in Marionettejs
define(['require', 'backbone.marionette', 'handlebars',
'constants/events', 'text!../templates/v_btn.html',
'css!../styles/buttons.css'],
function (require) {
'use strict';
var Backbone = require('backbone');
var Marionette = require('backbone.marionette');
var Handlebars = require('handlebars');
var Events = require('constants/events');