Skip to content

Instantly share code, notes, and snippets.

View petermoresi's full-sized avatar

Peter Moresi petermoresi

  • Greater Los Angeles
View GitHub Profile
@petermoresi
petermoresi / parse.js
Last active October 22, 2015 01:29
Convert mixed whitespace and comma into array
// Let's say you want a text field on a webpage
// where the user can enter a mix of comma delimited,
// white space delimited and line delimited.
//
// BUT you want the computer to read it as a flat list
var str = "1,2\n3 4";
str.replace(/,\W/g, ' ')
.replace(/,/g, ' ')
@petermoresi
petermoresi / NameList-React-Component.js
Last active May 1, 2023 01:59
A simple ES6 React component to render a list of names
import React from 'react';
class NameList extends React.Component {
constructor(props){
super(props);
}
render() {
var i = 1;
var list = this.props.names.map( (name) => {
@petermoresi
petermoresi / Makefile
Last active November 14, 2019 19:58
A Makefile for web developers using babel with webpack or browserify
# Makefile for web development with:
# 1. ES6 / Babel compiler
# setup: npm install babel
# 2. Bundler (Webpack or Browserify)
# setup: npm install webpack|browserify
# 3. Static Web Server
# setup: npm install http-server
WEBMAKE = webmake
WEBPACK = webpack
@petermoresi
petermoresi / minimal-react.html
Last active August 29, 2015 14:26
An HTML starter file for React development
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>Minimal React Setup</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.min.js" type="text/javascript"></script>
</head>
<body>
<script src="app.js" type="text/javascript"></script>
</body>
@petermoresi
petermoresi / digial-countdown-clock.js
Created October 22, 2015 01:15
digital countdown clock
var time = 1000;
function updateTimer() {
document.body.innerHTML = ('<div style="font-size: 6em">' + Math.round(time/60) + ":" + (--time%60) + '</div><br/><div>' + time + ' total seconds</div>')
}
var timerId = setInterval( updateTimer, 1000)
// reset with:
// clearInterval(timerId);
@petermoresi
petermoresi / make-pairs.js
Created October 22, 2015 03:57
Use reduce to chunk flat array into pairs
[1,1,2,2,3,3,4,4,5].reduce( function(a, b, i) {
if (i === 1) {
return [[a, b]];
} else {
if (i % 2 === 0) {
return a.concat([[b]]);
} else {
return a.splice(0, a.length-1).concat([a[a.length-1].concat(b)]);
}
}
@petermoresi
petermoresi / you-do-not-know-js.js
Last active October 23, 2015 02:45
alert( 'You don't know JavaScript' )
[]['map']['constructor']('s', 'alert(s); return true')('You don\'t know JavaScript')
@petermoresi
petermoresi / gist:a09ec44262ace28ff9e3
Created October 24, 2015 18:10
Convert require to new JS import statements
%s/var \(.*\) = require('\(.*\)');/import \1 from '\2';
@petermoresi
petermoresi / string-raw.js
Last active October 30, 2015 15:49
String.raw and ES6 template strings
let a = 1, b = 2, foo="foo", bar="bar", credentials: "insecure";
String.raw`http://foo.org/bar?a=${a}&b=${b}
Content-Type: application/json
X-Credentials: ${credentials}
{ "foo": ${foo},
"bar": ${bar}}`;
/* The code above produces the result
"http://foo.org/bar?a=1&b=2
@petermoresi
petermoresi / rule.jison
Last active September 27, 2016 21:47
A simple business rules language based on JISON
/* description: Parses end executes mathematical expressions. */
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
[0-9]+("."[0-9]+)?\b return 'NUMBER'
"true" return 'TRUE'
"false" return 'FALSE'