Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
lahmatiy / prevent-input.js
Last active May 22, 2023 17:19
Snippet for node.js to prevent any user input while script is working, but process ctrl+c to exit
// doesn't work w/o this interface creation
require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// stop process input
process.stdin.pause();
// read from stdin 10 times per second to check if ctrl+c pressed
const EXTENSION_TYPE = {
0x01: 'PlainText',
0xF9: 'GraphicControl',
0xFE: 'Comment',
0xFF: 'Application'
};
/**
* Returns total length of data blocks sequence
*
const csstree = require("css-tree");
const cssDefinitions = ["color"];
const ast = csstree.parse(`
.some-selector {
color: black;
border: 1px solid #fff;
}
`, { parseValue: false });
csstree.walk(ast, {
@lahmatiy
lahmatiy / get-selector-parent.js
Created January 1, 2019 21:41
Get a selector parent selector with csstree (an issue https://twitter.com/peterbe/status/1079854825880326144)
const cssTree = require("css-tree@1.0.0-alpha.29");
function getSelectorParent(selector) {
const selectorAst = cssTree.parse(selector, { context: 'selector' });
// solution #1
selectorAst.children.prevUntil(selectorAst.children.tail, (node, item, list) => {
list.remove(item);
return node.type === 'Combinator' || node.type === 'WhiteSpace';
});
const cssTree = require("css-tree@1.0.0-alpha.29");
function getSelectorParent(selector) {
const selectorAst = cssTree.parse(selector, { context: 'selector' });
// solution #1
// selectorAst.children.prevUntil(selectorAst.children.tail, (node, item, list) => {
// list.remove(item);
// return node.type === 'Combinator' || node.type === 'WhiteSpace';
// });
const cssTree = require("css-tree@1.0.0-alpha.29");
function getSelectorParent(selector) {
const selectorAst = cssTree.parse(selector, { context: 'selector' });
// solution #1
// selectorAst.children.prevUntil(selectorAst.children.tail, (node, item, list) => {
// list.remove(item);
// return node.type === 'Combinator' || node.type === 'WhiteSpace';
// });
@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();
@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 / 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 / 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;
}