Skip to content

Instantly share code, notes, and snippets.

@mjzone
mjzone / dynamicallyTyped.js
Last active July 3, 2016 05:30
Example 01
var myString = "Manoj";
var myNumber = 42;
myString = 32;
console.log('42' - 2); // Result: 40
console.log('42' + 2); // Result: 422 gotcha! (Javascript infers the type of variable at Runtime.)
// Javascript assumes you want to concat two strings.
var anotherString = '42'
console.log(parseInt(anotherString, 10) + 2); // Be explicit about your variable types.
console.log('Manoj'.length); // 5 (String is an object. So you can access its properties.)
var person = new Object();
person.name = 'Manoj';
console.log(person) //Result: Object {name: "Manoj"}
var ages = new Array(10);
console.log(ages) // Result: [,,,,,,,,,,] gotcha!
// Always use JSON format.
var person = {
name: 'Manoj'
// Functions are 'first-class' objects.
var myFunction = function(){
console.log("I have been called.");
}
myFunction.firstName = 'Manoj'; // we can attach attributes to functions because they are also objects.
console.log(myFunction);
// Functions can be anonymous.
setTimeout(function(){
console.log("hello, i have been called after 1 second.")
@mjzone
mjzone / example1.js
Created August 31, 2016 23:44
Double the array
var array1 = [1,2,3];
var array2 = [];
for(var i=0; i< array1.length; i++){
array2.push(array1[i]*2);
}
console.log(array2);
@mjzone
mjzone / README.md
Created December 23, 2017 04:41 — forked from leonardofed/README.md
A curated list of AWS resources to prepare for the AWS Certifications


A curated list of AWS resources to prepare for the AWS Certifications

A curated list of awesome AWS resources you need to prepare for the all 5 AWS Certifications. This gist will include: open source repos, blogs & blogposts, ebooks, PDF, whitepapers, video courses, free lecture, slides, sample test and many other resources.

For more about AWS and AWS Certifications and updates to this Gist you should follow me @leonardofed


@mjzone
mjzone / authorize.js
Created February 14, 2018 23:59 — forked from kndt84/authorize.js
Sample code: how to refresh session of Cognito User Pools with Node.js and Express
const AWS = require('aws-sdk');
const CognitoUserPool = require('amazon-cognito-identity-js-node').CognitoUserPool;
const CognitoUserSession = require('amazon-cognito-identity-js-node').CognitoUserSession;
const CognitoUser = require('amazon-cognito-identity-js-node').CognitoUser;
const CognitoIdToken = require('amazon-cognito-identity-js-node').CognitoIdToken;
const CognitoAccessToken = require('amazon-cognito-identity-js-node').CognitoAccessToken;
const CognitoRefreshToken = require('amazon-cognito-identity-js-node').CognitoRefreshToken;
const cfg = require('config').config;
const COGNITO_IDENTITY_POOL_ID = cfg.COGNITO_IDENTITY_POOL_ID;
@mjzone
mjzone / migrate.js
Created April 14, 2020 10:27
User Migration Example
let _ = require('underscore');
let USERS_TABLE = process.env.USERS_TABLE;
module.exports.migrateUser = async (event, context) => {
if (event.triggerSource == 'UserMigration_Authentication') {
// check if the user exists on mlab
if (event.userName && event.userName.length) {
let db = await dbHelper.connectToDatabase(MONGODB_URI, DB_NAME);
// lookup on the mongodb
lookUp(db, event.userName).then(
@mjzone
mjzone / public.js
Last active June 24, 2020 17:12
public access level
// The default access level of the uploads is "public"
const handleChange = async (e) => {
const file = e.target.files[0];
await Storage.put('picture.jpg', file, {
contentType: 'image/jpg'
});
}
// HTML
<input type="file" accept='image/jpg' onChange={(evt) => handleChange(evt)} />
// Set the "level" attribute value to "protected"
const handleChange = async (e) => {
const file = e.target.files[0];
await Storage.put('picture.jpg', file, {
contentType: 'image/jpg',
level: 'protected'
});
}
// HTML