Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
lahmatiy / refs.js
Created September 10, 2015 20:59
Find the difference
// react 0.14-rc1
var Zoo = React.createClass({
render: function() {
return <div>Giraffe name: <input ref="giraffe" /></div>;
},
showName: function() {
// Previously: var input = this.refs.giraffe.getDOMNode();
var input = this.refs.giraffe;
alert(input.value);
}
@lahmatiy
lahmatiy / bem-scopes-example.js
Last active April 16, 2016 10:57
Example of css scopes building
var csso = require('csso');
function splitByScope(css) {
var scopes = {};
csso.walk(csso.parse(css), function(node) {
if (node.type === 'Class') {
var className = node.name;
var scopeId = className.replace(/^([^_]+)_.+/, '$1'); // scopeId is block name
@lahmatiy
lahmatiy / createBlockClass.js
Created July 27, 2016 13:17
Implementation of `model` and `visible` for from fields in basis.js
var events = require('basis.event');
var resolveValue = require('basis.data').resolveValue;
module.exports = function createBlockClass(BaseClass, ext) {
return BaseClass.subclass(ext, {
visible: true,
visibleRA_: null,
emit_visibleChanged: events.create('visibleChanged'),
// value could be a function or binding-bridge instance; any other converts to bool
setVisible: function(value) {
// Unique with use object
function uniqueObject(array) {
var hash = Object.create(null);
var result = [];
var value;
for (var i = 0; i < array.length; i++) {
value = array[i];
if (!hash[value]) {
hash[value] = true;
//
// Case 1
//
var a = [];
var t = performance.now();
for (var i = 0; i < 10000; i++)
a.push(new Promise(function(){}));
@lahmatiy
lahmatiy / results.md
Last active October 8, 2017 20:47
JS perf quiz

Similar solutions with very different execution times. What's the reason? Why solution #1 is too slow?

Hint: access to .length property and to variables is not an issue.

Based on a real case in CSSTree

Node v6.11.3

N Solution #1 Solution #2
@lahmatiy
lahmatiy / check-initial.js
Created January 7, 2018 23:33
Check mdn/data initial values
const csstree = require('css-tree');
const data = require('mdn-data/css/properties.json');
Object.keys(data).forEach(name => {
const prop = data[name];
if (Array.isArray(prop.initial)) {
return;
}
@lahmatiy
lahmatiy / concat-ast.js
Created April 11, 2018 16:07
Concat AST with CSSTree. Note that CSSTree lost an original formatting on parsing.
const csstree = require('css-tree');
const ast1 = csstree.parse('.a { color: red }', {
filename: './a.css',
positions: true
});
const ast2 = csstree.parse('.b { color: red }', {
filename: './b.css',
positions: true
});
@lahmatiy
lahmatiy / png.inject.js
Last active April 16, 2018 13:19
Solution to inject/fetch a custom data to/from a PNG image
const pngSignature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
const hashKey = 'react-snapshot-hash';
const crcTable = [];
const initialCrc = 0xffffffff;
for (let n = 0; n < 256; n++) {
let c = n;
for (let k = 0; k < 8; k++) {
if (c & 1) {
@lahmatiy
lahmatiy / prop-types-patch.js
Last active July 9, 2018 20:01
webpack loader to patch the prop-types to get an information about the propTypes definition in a runtime (UPD: you can use ready to use package https://github.com/avito-tech/prop-types-definition)
// UPDATE: you can use ready to use package prop-types-definition
// https://github.com/avito-tech/prop-types-definition
module.exports = function(content) {
return content + `
(function(ReactPropTypes) {
function unwrapValueItem(value) {
if (value) {
if (typeof value.getTypeDefinition === 'function') {
return value.getTypeDefinition();