I HOPE this code is ES6 it IS JavaScript 🙂

GOOD STUFF:

Python VS ES6 syntax comparison

Contents

  1. Idioms
  2. Ajax Template

Idioms

/* BUMMERS:
  - as of Nov 2, 2017 import statements are NOT in Node.js LTS
  - ES6 postponed Array comprehensions (likePython).
        (https://stackoverflow.com/a/35637068/601770)
  - typeof object name discovery: https://stackoverflow.com/q/332422/601770
  - google: es6 prototypes vs closures vs classes
    - NOT LIKE: Zen of Python is, "There should be one — and preferably -
        only one obvious way to do it."
*/

/* Destructuring assignment [array or object](https://goo.gl/LBQ5eC) */

// array
var a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10

// object 
var o = {p: 42, q: true};
var {p, q} = o;
console.log(p); // 42
console.log(q); // true

/* read to string one liner */
var mystr= fs.readFileSync("./mytextfile.txt").toString('utf-8')

/* Re-direct redir console.log  https://stackoverflow.com/a/45291964/601770 */
const out = fs.createWriteStream('./stdout.log') 
const err = fs.createWriteStream('./stderr.log')
const jconsole = new console.Console(out, err);
jconsole .log('54-40 or fight!')


/* readdirSync */
let items = fs.readdirSync(nextSundayEmailDirPath)
for (let value of items ) {
    console.log('value: '+value)
    if (value.endsWith('.htm')){nextSundayEmailFilename = value;
} 

/* for-of (need also for-in, forEach, ) */
let myIterable = [10, 20, 30]

for (let value of myIterable ) {
  value += 1
  console.log(value)
}
// 11
// 21
// 31

/* timestamp */
let ts = (new Date().toISOString()).replace(/:/g, '.').replace('T', '__').slice(0, -5)
// 2017-11-13__18.33.16

/* string format %s   */
const util = require('util')
util.format('Timestamp: %s', '2017-11-13_18.26.52')
// 2017-11-13_18.26.52

/* slice NOT same as Python */
 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

> s.slice(null)
'HelpA'
> s.slice(0)
'HelpA'
> s.slice(-1)
'A'
> s.slice(-5)
'HelpA'
> s.slice(null,null)
''
> s.slice(null, 5)
'HelpA'
> s.slice(null, 4)
'Help'
> s.slice(0, 4)
'Help'
> s.slice(0, -2)
'Hel'

/** ES6 string functions & string features 

2ality.com/2015/01/es6-strings.html
exploringjs.com/es6/ch_strings.html

**/

/* ES6 String Templates & Multi-line String Literals */
// see https://stackoverflow.com/a/32695337/601770
//     https://developers.google.com/web/updates/2015/01/ES6-Template-Strings
let name = 'Fred'
let tm = `Dear ${name},
This is to inform you, ${name}, that you are
IN VIOLATION of Penal Code 64.302-4.
Surrender yourself IMMEDIATELY!
THIS MEANS YOU, ${name}!!!
`
console.log(tm)
// OUTPUT:
Dear Fred,
This is to inform you, Fred, that you are
IN VIOLATION of Penal Code 64.302-4.
Surrender yourself IMMEDIATELY!
THIS MEANS YOU, Fred!!!

/* ES6 String substr & substring */
// see https://stackoverflow.com/a/3745518/601770
> let s = 'href="https://docs.g.com/doc/d/1lRuC1p7TXRbmI/edit?usp=sharing_eil&ts=5a1f0fbf"'

> s.substring(6)  // same as s.substr(6)
'https://docs.g.com/doc/d/1lRuC1p7TXRbmI/edit?usp=sharing_eil&ts=5a1f0fbf"'
> s.substring(6) === s.substr(6)
true

// HOWEVER with 2nd arg false
> s.substring(6, s.length-1) === s.substr(6, s.length-1)
false

> s.substring(6, s.length-1) // 2nd arg is index to stop at (but not include)
'https://docs.g.com/doc/d/1lRuC1p7TXRbmI/edit?usp=sharing_eil&ts=5a1f0fbf'

> s.substr(6, s.length-1) // 2nd arg is the maximum length to return
'https://docs.g.com/doc/d/1lRuC1p7TXRbmI/edit?usp=sharing_eil&ts=5a1f0fbf"'

/* get string between 2 strings  (lstr & rstr) */
function between(instr, lstr, rstr){return instr.split(lstr)[1].split(rstr)[0]}

var emailURLTest = 'https://docs.google.com/document/d/1lRuCJUNK_IN_HEREbgbeCZKGeg4yfdfnw7TXRbmI/edit?usp=sharing_eil&ts=5aJUNK2bf'

between(emailURLTest, 'https://docs.google.com/document/d/', '/edit?')

// '1lRuCJUNK_IN_HEREbgbeCZKGeg4yfdfnw7TXRbmI'

/* [JSON  parse & stringify](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON) */

// parse
var superHeroesText = fs.readFileSync("./superHeroesText.txt").toString('utf-8')
var superHeroes = JSON.parse(superHeroesText)

// stringify
var myJSON = { "name": "Chris", "age": "38" };
myJSON
var myString = JSON.stringify(myJSON);
myString

/* node.js modules ( != es6 modules )
https://www.w3schools.com/nodejs/nodejs_modules.asp
Content:
    // importing code in app.js
    // exporting code in j_utils.js
    // OUTPUT
*/

// importing code in app.js
let util = require('./j_utils.js')
console.log(util.myTimeStamp())
console.log(util.cwd())

// exporting code in j_utils.js
exports.myTimeStamp= function () {
    let ts = (new Date().toISOString()).replace(/:/g, '.').replace('T','__').slice(0, -5)
    return ts
}
exports.cwd= function () {
    return process.cwd()
}

// OUTPUT
TrinitySundayMusic$ node app.js
2017-11-22__16.33.41
/mnt/c/1d/TrinitySundayMusicPj/TrinitySundayMusic
TrinitySundayMusic$

/* like Python if __main__ */

my_main = function () {
    console.log('In my_main()')
}
exports.getMusicURL = function () {
    let music_url = my_main()
    return music_url
}
// run from node
if (require.main === module) {
    my_main()
}

/* END Idioms */

Ajax Template


Advertisements