Skip to content

Instantly share code, notes, and snippets.

View kangax's full-sized avatar

Juriy Zaytsev kangax

View GitHub Profile

RegExp.escape(string)

Computes a new version of a String value in which certain characters have been escaped, so that the regular expression engine will interpret any metacharacters that it may contain as character literals.

When the escape function is called with one argument string, the following steps are taken:

  1. Let string be ToString(string).
  2. ReturnIfAbrupt(string).
  3. Let length be the number of characters in string.
  4. Let R be the empty string.
@kangax
kangax / gist:a02bcb082cd8a7d36d0a
Last active August 29, 2015 14:02
Chrome DevTools suggestions
  • Being able to edit JS source after prettifying #103143

  • Jump to function definition (or see prettifyed function definition) #248647

  • Better JS prettifying (e.g. newlines after ",") #383933

  • Cmd/Ctrl + P in sources to do fuzzy matching (e.g. "all js" doesn't find "all.js", like Sublime does) #383945

  • Smarter console auto-suggest

    • alert(console.) doesn't show suggestions
function formatSchedule(schedules) {
var dayAbbr = [ 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс' ];
function dayIndicesToWord(indices) {
if (areDayIndicesSequential(indices)) {
return dayAbbr[indices[0]] + '-' + dayAbbr[_.last(indices)];
}
else {
var daysAsWords = _.map(indices, function(index) {
return dayAbbr[index];
function formatSchedule(schedules) {
var dayAbbr = [ 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс' ];
function dayIndicesToWord(indices) {
if (areDayIndicesSequential(indices)) {
return dayAbbr[indices[0]] + '-' + dayAbbr[_.last(indices)];
}
else {
var daysAsWords = _.map(indices, function(index) {
return dayAbbr[index];
@kangax
kangax / BEM.html
Last active August 29, 2015 14:06
<!--
<component-name>[--modifier-name|__descendent-name]
.component {}
.component__descendant {}
.component--modifier {}
-->
<article class="tweet tweet--is-expanded">
<header class="tweet__header">
var TodoApp = Backbone.View.extend({
template: Handlebars.compile(
'<div>\
<h3>TODO</h3>\
{{#each items}}\
{{ this }}\
{{/each}}\
<form>\
<input value="{{ text }}">\
<button>Add #{{ items.length }}</button>\
var Person = Class.create({
initialize: function(name) {
this.name = name;
}
})
Element.Methods.descendantOf = (function() {
var doc = document.documentElement;
// DOM Level 3 approach (Gecko, Opera)
if ('compareDocumentPosition' in doc) {
return function(element, ancestor) {
return (element.compareDocumentPosition(ancestor) & 8) === 8
};
}
// IE, Safari
function cmpVersion(a,b) {
var ai, bi;
if (a.startsWith('.')) a = '0' + a;
if (b.startsWith('.')) b = '0' + b;
a = a.split('.');
b = b.split('.');
console.log(a,b)
for (var i=0, l=Math.max(a.length, b.length); i<l; i++) {
ai = typeof a[i] == 'undefined' ? 0 : parseInt(a[i]);
bi = typeof b[i] == 'undefined' ? 0 : parseInt(b[i]);
(function() {
var getText, setText;
if ('innerText' in document.documentElement) {
getText = function(element, shallow) {
return (!shallow) ? $(element).innerText : getText(element);
}
setText = function(element, text) {
(element = $(element)).innerText = text;