RegEx search/replace with group

If we have this code from a random Gulp file:

babelify : require('babelify'),
browserify : require('browserify'),
buffer : require('vinyl-buffer'),
changed : require('gulp-changed'),
collapse : require('bundle-collapser/plugin'),
concat : require('gulp-concat'),
cssnano : require('gulp-cssnano'),
declare : require('gulp-declare'),
gulp : require('gulp'),
gulp_handlebars : require('gulp-handlebars'),
gutil : require('gulp-util'),
htmlclean : require('gulp-htmlclean'),
jsdoc : require('gulp-jsdoc3'),
merge : require('merge-stream'),
path : require('path'),
rename : require('gulp-rename'),
sass : require('gulp-sass'),
source : require('vinyl-source-stream'),
sourcemaps : require('gulp-sourcemaps'),
transform : require('vinyl-transform'),
uglify : require('gulp-uglify'),
watchify : require('watchify'),
wrap : require('gulp-wrap'),

And quickly want to run an npm install using these modules.

We can run a search and replace with Sublime Text using this formular:

Find:

(.*) : require\('(.*)'\),
/invisible new line character here

Replace:

$2 /invisible space character here

This will find:

<any number of any character> : require('<any number of any character>'),

And replace it with the content of the second group (.*) represented by:

$2

You can now copy the whole line, and type:

npm i (and paste the clipboard content here)

into the terminal.

Voila!

 

A comma separated string to console.log();

If you have this:

, type, name, declaration, options, callback, internal, useRequired, skipEmit, uptodateName, next, packageName

Then find this:

, ([^,]+)

Replace with:

console.log( "$1", $1 );

This:

, type, name, declaration, options, callback, internal, useRequired, skipEmit, uptodateName, next, packageName

becomes this:

console.log( "type", type );
console.log( "name", name );
console.log( "declaration", declaration );
console.log( "options", options );
console.log( "callback", callback );
console.log( "internal", internal );
console.log( "useRequired", useRequired );
console.log( "skipEmit", skipEmit );
console.log( "uptodateName", uptodateName );
console.log( "next", next );
console.log( "packageName", packageName );

 

PostgreSQL

Install with Homebrew:

$ brew install postgresql

Start postgresql as a service

$ brew services start postgresql

Enter Queries using psql:

$ psql postgres

Create database:

$ CREATE DATABASE database_name

Add table:

$ ALTER DATABASE database_name

(press enter, and do not end with a semi-colon. This will make it possible to enter multiple lines).

$ ADD COLUMN id integer;

Set id as primary key:

$ ALTER TABLE table_name

(press return)

$ ADD PRIMARY KEY (column_name);

 

Class: Cookie

class Cookie {
    constructor () {}

    set( cname, cvalue, exdays ) {
        var d = new Date();
        d.setTime( d.getTime() + ( exdays*24*60*60*1000 ) );
        var expires = "expires="+ d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

    get( cname ) {
        var name = cname + "=";
        var decodedCookie = decodeURIComponent( document.cookie );
        var ca = decodedCookie.split( ';' );
        for( var i = 0; i < ca.length; i++ ) {
            var c = ca[ i ];
            while ( c.charAt( 0 ) == ' ' ) {
                c = c.substring( 1 );
            }
            if ( c.indexOf( name ) == 0 ) {
                return c.substring( name.length, c.length );
            }
        }
        return "";
    }
}

export default Cookie;

 

Compile Handlebars templates and partials in Gulp

var gulp        = require('gulp'),
    uglify        = require('gulp-uglify'),
    rename        = require('gulp-rename'),
    concat        = require('gulp-concat'),
    handlebars    = require('gulp-handlebars'),
    wrap        = require('gulp-wrap'),
    path        = require('path'),
    htmlclean    = require('gulp-htmlclean'),
    merge        = require('merge-stream'),
    declare        = require('gulp-declare'),
    source        = require('vinyl-source-buffer'),

/**
 * Compile handlebars templates
 */
gulp.task('templates', function () {
    // Assume all partials start with an underscore
    // You could also put them in a folder such as source/templates/partials/*.hbs
    var partials = gulp.src( partials_source )
        .pipe( htmlclean() )
        .pipe( handlebars())
        .pipe( wrap('Handlebars.registerPartial(<%= processPartialName(file.relative) %>, Handlebars.template(<%= contents %>));', {}, {
            imports: {
                processPartialName: function(fileName) {
                    // Strip the extension and the underscore
                    // Escape the output with JSON.stringify
                    return JSON.stringify(path.basename(fileName, '.js').substr(1));
                }
            }
        }));
        
    var templates = gulp.src( templates_source )
        .pipe( htmlclean() )
        .pipe( handlebars() )
        .pipe( wrap('Handlebars.template(<%= contents %>)') )
        .pipe( declare({
            namespace : 'Publish.templates',
            noRedeclare : true
        }));

    return merge(partials, templates)
        .pipe( uglify() )
        .pipe( concat( 'publish.templates.js' ) )
        .pipe( rename( { suffix : '.min' } ) )
        .pipe( gulp.dest('./publish/assets/js') );
});

 

Passing data to Handlebars partials

If you have this:

const dummy_data = {
                toplinks : ['Shop','Publish','Read'],
                is_staff : true,
                latam : false,
                user : {
                    is_authenticated : true,
                    first_name : "John",
                    last_name : "Doe"
                },
                currentLanguage : 'DA',
                language : [
                    'DA',
                    'EN'
                ]
            };

        var template = Publish.templates.navigation,
            output = template( dummy_data );

            $('body').prepend( output );

Do this in the main template:

{{> toplinks toplinks=this.toplinks staff=this.is_staff}}

And this in the partial:

{{#each toplinks}}
    <a href="{{this}}" target="_blank" class="nav-btn">{{this}}</a>
{{/each}}

{{#if staff}}
    <a href="#" target="_top" class="nav-btn">Admin</a>
{{/if}}