Skip to content

Instantly share code, notes, and snippets.

@Shwartz
Shwartz / gulp.js
Last active January 10, 2017 14:51
gulp task to create index file with a list of all files - TOC
/*
* Utility to create index file with a list of all files - TOC
* */
gulp.task('create-toc', function () {
var fs = require('fs');
var fileList = '';
fs.readdir('pages/cats', function(err, files) {
files.forEach(function (file) {
fileList += '<li><a href="'+ file +'">'+ file +'</a></li>';
@Shwartz
Shwartz / gulp.js
Last active January 10, 2017 14:50
gulp webpack example with flag to run different configs
gulp.task('webpack', function () {
console.log('TASK:WEBPACK');
/*
* run in gulp: gulp webpack --target=prod
* to create prod JS
* */
if (gutil.env.target == 'prod') {
webpackConfig.plugins = [
new webpack.optimize.DedupePlugin(),
@Shwartz
Shwartz / gulp.js
Last active January 10, 2017 14:49
Noje.js Gulp rename files
gulp.task('rename', function () {
var fs = require('fs');
var fileList = '';
fs.readdir('sass/test', function(err, files) {
files.forEach(function (file) {
console.log('file: ', file);
fs.rename('sass/test/' + file, 'sass/test/_' + file, function (err) {
if (err) console.log('ERR: ' + err);
})
@Shwartz
Shwartz / elPosition.js
Last active January 10, 2017 14:54
JavaScript: Find element position by looping from element up to body tag
// Original source: https://www.kirupa.com/html5/get_element_position_using_javascript.htm
// Helper function to get an element's exact position
function getPosition(el) {
var xPos = 0;
var yPos = 0;
while (el) {
if (el.tagName == "BODY") {
// deal with browser quirks with body/window/document and page scroll
var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
@Shwartz
Shwartz / gulp.js
Created January 10, 2017 15:01
Using Gulp and Node to change file content based on a string pattern
gulp.task('update-content-file', function () {
"use strict";
var fs = fs || require('fs');
var path = 'path/to/folder';
// example of CSS path change and adding new css
var oldContent = /<link href="(.*)dev\/css\/main.css" rel="stylesheet">/;
var newContent = '<link href="/dev/css/main.css" rel="stylesheet"><link href="/dev/css/specific.css" rel="stylesheet">';
fs.readdir(path, function(err, files) {
@Shwartz
Shwartz / listener.js
Created January 10, 2017 16:05
addEventListner with requestAnimationFrame
(function() {
"use strict";
var ticking = false;
window.addEventListener('scroll', function () {
if (!ticking) {
window.requestAnimationFrame(function () {
runYourMethod();
ticking = false;
});
@Shwartz
Shwartz / events.js
Created February 13, 2017 16:44
For testing clickable elements add preventDefault() to all of them
window.addEventListener("load", function () {
console.warn('------------------- EAGLE LANDED -------------------')
let arr = Array.from(document.getElementsByTagName('a'));
console.warn('A TAGS', arr.length);
arr.forEach(function (a) {
a.addEventListener('click', function (e) {
console.warn('CLICK ELEM', e.target);
e.preventDefault();
});
});
(function () {
const subClassExample = {
Person: function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = 'male';
}
}
const clark = new subClassExample.Person('Clark', 'Kent');
@Shwartz
Shwartz / gulpfile.js
Created December 4, 2017 11:47
Mini handlebar with a Gulp. Works similar like php include(), can pass variable as well
/**
Task will run through /src/html/ folder and will loop through each html file
When it finds {{head}}, it will be replaced with a content from /src/inc/head.html
Example of src/inc/breadcrumb.html
<ul>
<li><a href="link/page">Home</a></li>
<li>{{content}}</li>
</ul>
@Shwartz
Shwartz / sorting.js
Last active June 20, 2018 10:16
ES6 style sorting helpers for objects
const asc = (left, right) => left < right ? -1 : left > right ? 1 : 0;
const desc = (left, right) => left > right ? -1 : left < right ? 1 : 0;
const sort = (array, column, order) => array.sort(({[column]: left}, {[column]: right}) => order(left, right));
export const sortAsc = (array, column) => sort(array, column, asc);
export const sortDesc = (array, column) => sort(array, column, desc);