AWS Lambda testing: dump of notes so far

Lessons: node 6.10.3. Don’t try the current version – it won’t work.
In my js/noob world, Lambda-tester is looking the simplest way so far to test AWS Lambdas. I’m not quite there yet, but will return to this very soon.

https://www.npmjs.com/package/lambda-tester

  Id CommandLine
 -- -----------
 1 node -v
 2 npm -v
 3 function prompt(){}
 6 pwd
 7 cd C:\Github\Alexa\MagContact\src\PackageSet\test
 8 pwd
 9 ls
 10 mocha
 11 cd ..
 12 pwd
 13 mocha
 15 npm install lambda-tester --save-dev
 16 npm install nonsense --save-dev
 17 npm install nonsensex --save-dev
 18 npm install lambda-tester --save-dev
 19 ls
 20 mocha
 21 npm install mocha -g
 23 mocha
 24 Get-History

6.10.3

http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html

https://nodejs.org/en/blog/release/v6.10.3/

Windows 32-bit Installer: https://nodejs.org/dist/v6.10.3/node-v6.10.3-x86.msi
Windows 64-bit Installer: https://nodejs.org/dist/v6.10.3/node-v6.10.3-x64.msi
Windows 32-bit Binary: https://nodejs.org/dist/v6.10.3/win-x86/node.exe
Windows 64-bit Binary: https://nodejs.org/dist/v6.10.3/win-x64/node.exe

https://nodejs.org/dist/v6.10.3/node-v6.10.3-x64.msi

https://github.com/mapbox/lambda-cfn/issues/54

 

 

Advertisements

JavaScript and ES6: looping

As I am pretty much a noob at JavaScript (Alexa-related posts passim), I thought makes sense to incorporate the use of ES6 from the start. So I need to loop through an array… does ES6 bring anything new to the table on that? Googled that, found this.

The author and the series looks a good place to consult for ES6, so will return to his posts.

And for looping, this is the answer:

for (var value of myArray) {
 console.log(value);
}

 

JavaScript: some lessons learned

Checking for valid dates

A naive solution:

Minimal web server

As discussed yesterday, and not related to JS as such, but useful:

Get a count of the instances of a given “class” in the DOM

You have to walk those instances 1 by 1, getting the innerHTML or innerTEXT. As far as I can see, you can’t just aggregate it all up:

ES6 syntax: for / let /of

Sorting an array of objects by a single attribute, in this case:

Templates…

Do stuff with an object, then add it to the array:

The 2 parts needed to make sense of a linked HTML:


'use strict';
// entry point...
(function () {
var link;
var content;
var composers = [];

importComposers();

var composerSetCount = content.getElementsByClassName("ComposerSet").length;

for (var index = 0; index < composerSetCount; index++) {
var composerSet = content.getElementsByClassName("ComposerSet")[index].innerHTML;
var composersAsElements = htmlToElements(composerSet);

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
for (let composerAsElements of composersAsElements) {
convertComposerTextToObject(composerAsElements, composers);
};
}

composers.sort(function(a,b) {return (a.lastName > b.lastName) ? 1 :
((b.lastName > a.lastName) ? -1 : 0);});

for (let comp of composers) {
var dates = [];
dates = validateComposerDates(comp);

document.writeln("
<h1>" + comp.lastName + "</h1>
");
document.writeln("
<h4>" + comp.firstNames + " " + comp.lastName + " " +
dates["Birth"] + " " + dates["Death"] + "</h4>
");
}

// functions...
function htmlToElements(html) {
var template = document.createElement('template');
template.innerHTML = html;
return template.content.childNodes;
}

function convertComposerTextToObject(composer, composersx) {
var composerText = composer.innerText;
if (composerText === undefined) {
return;
}
// Treat the characters , and [ and - and ] as delimiters for splitting the string into tokens.
// An example of a string to be split:
// SMITH, John [4/21/1712-7/9/1768]
var separators = [',', '\\[', '\\-', '\\]']
console.log(composerText);
var composerParts = composerText.split(new RegExp('[' + separators.join('') + ']', 'g'));

var theComposer = {};
theComposer.lastName = composerParts[0];
theComposer.firstNames = composerParts[1];
var dob = new Date(composerParts[2]);
if (dob.isValid()) {
theComposer.dateOfBirth = dob;
}

var dod = new Date(composerParts[3]);
if (dod.isValid()) {
theComposer.dateOfDeath = dod;
}

composersx.push(theComposer);
console.log(theComposer);

} // convertComposerTextToObject

function importComposers() {
link = document.querySelector('link[rel="import"]');
content = link.import;
}

function validateComposerDates(currentComposer) {
var composerDates = [];
composerDates['Birth'] = currentComposer.dateOfBirth === undefined ? "[unknown]" : currentComposer.dateOfBirth.toLocaleDateString('en-GB');
composerDates['Death'] = currentComposer.dateOfDeath === undefined ? "[unknown]" : currentComposer.dateOfDeath.toLocaleDateString('en-GB');
//console.log(composerDates);
return composerDates;

}

})();

HTML, Angular, Json

This is a variation on an earlier page that actually has a purpose. I used to chuck my favourite links into a piece of html.. well I’m still doing that, but at least I’ve separated the data from the code. I wanted to have a separate file for the Json, but in static pages, you can’t easily do that for security reasons, and I don’t want to spin up IIS etc for something so simple.

This is the end result (so you just keeping adding to the Json):

<pre><!DOCTYPE html>
<html ng-app="linksApp">
<head>
<title>My name is John</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body ng-controller="linksCtrl as ctrl">
<div class="container" ng-cloak>
<h2>{{ctrl.Summary}} (Count:{{ctrl.Links.length}})</h2>
<table class="table table-condensed">
<tr>
<th>Link</th>
</tr>
<tr ng-repeat="links in ctrl.Links">
<td><a href="{{links.URL}}">{{links.Name}}</a></td>
</tr>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.js"></script>
 <script type="application/javascript">
 var linksApp = angular.module("linksApp",[]);
 linksApp.controller("linksCtrl", [function() {
 vm = this;
 vm.Summary = "A Set of Links";
 vm.Links = Links;
 }]);
 </script>

</body>
</html>
<!-- data... -->
<script>
 var Links = [
 {"Name" : "My blog", "URL" : "https://dennisaa.wordpress.com"},
 {"Name" : "Foreigner - Double Vision - YouTube", "URL" : "https://www.youtube.com/watch?v=36hdcn_3ekI"},
 {"Name" : "Acer 27\" monitor", "URL" : "http://www.acerdirect.co.uk/Acer_K272HLbid_69cm_27_Wide_6ms_100M_1_ACM_300nits_VA_LED_DVI_w-HDCP_HDMI_UM.HW3EE.005/version.asp"},
 {"Name" : "Chords for Hazard", "URL" : "https://tabs.ultimate-guitar.com/r/richard_marx/hazard_crd.htm"},
 ];
</script>

Visual Studio Code: Intellisense for Angular

Firstly, credit to this person for getting me to a certain point. However, after attempting this line from the steps:

typings install angular --ambient --save

, I was rewarded with this message:

The GitHub URL looked promising, so I took a look there. More drilling takes you here:

And if you read the contribution from said blakeembrey, you get the normal thing of some man or woman heroically fighting a tide of requests…  in their own time. Blake Embrey: I salute you! 🙂

So anyway, Blake quite reasonably suggests looking at the ReadMe (remember to scroll down when you get to the link). In there, we find this nugget:

Let’s take that principle, look for the Typings package for Angular, and install it:

typings search --name angular
typings install dt~angular --global --save

(So the only change really is the [dt~])

Now, we’ll close and re-open VSC, just in case, start our JavaScript file, and see what happens (spoiler: it works):

Typings09

I didn’t show you the before picture – you’ll just have to trust me, you won’t get IntelliSense for Angular out of the box in Visual Studio Code