Skip to content

Instantly share code, notes, and snippets.

@olvnikon
olvnikon / function-to-window.js
Created October 6, 2017 11:24
[AST + JSShiftCode] - "function name" to "window.name = function"
function isOnTheBody(path) {
return path.scope.parent.isGlobal;
}
function transform(j, path) {
const propName = path.value.id.name;
const params = path.value.params;
const body = path.value.body;
j(path).replaceWith(
@olvnikon
olvnikon / directive-to-component-transform.js
Last active October 6, 2017 11:30
[AST + JSShiftCode] - AngularJS 1.x directive to component
const DIRECTIVE_TO_COMPONENT = {
scope: "bindings"
};
const DEPRECATED_PROPS = ["restrict", "replace", "bindToController"];
const DIRECTIVE_UNIQUE_PROPS = ["link", "compile", "multiElement", "priority", "templateNamespace", "terminal"];
function transform(j, path) {
const directiveDeclaration = path.value.arguments[1];
const directiveProperties = directiveDeclaration.body.body[0].argument.properties;
const newProps = directiveProperties.filter(prop => !DEPRECATED_PROPS.includes(prop.key.name));
@olvnikon
olvnikon / 1-one-row-expressions.js
Last active April 16, 2019 06:48
Javascript tricky questions. Check yourself. Or destroy any interviewee.
// https://jsfiddle.net/vk35ok2o/50/
console.log('typeof 1/0 => ', typeof 1/0);
console.log('typeof (1/0) => ', typeof (1/0));
console.log('1/0 => ', 1/0);
console.log('"Hello" * 2 => ', "Hello" * 2);
console.log('typeof ("Hello" * 2) => ', typeof ("Hello" * 2));
console.log('typeof "Hello" * 2 => ', typeof "Hello" * 2);
console.log('typeof undefined => ', typeof undefined);
console.log('typeof null => ', typeof null);
console.log('typeof function(){} => ', typeof function(){});