Named exports/Name imports, export default

Tags

,

// airplane.js
// Export Named Exports

export let availableAirplanes = [
{name: 'AeroJet',
 fuelCapacity: 800,
 availableStaff: ['pilots', 'flightAttendants', 'engineers', 'medicalAssistance', 'sensorOperators'],
 maxSpeed: 1200,
 minSpeed: 300
}, 
{name: 'SkyJet',
 fuelCapacity: 500,
 availableStaff: ['pilots', 'flightAttendants'],
 maxSpeed: 800,
 minSpeed: 200
}
];

export let flightRequirements = {
  requiredStaff: 4,
  requiredSpeedRange: 700
};

export function meetsStaffRequirements(availableStaff, requiredStaff) {
  if (availableStaff.length > requiredStaff) {
    return true;
  } else {
    return false;
  }
};

export function meetsSpeedRangeRequirements(maxSpeed, minSpeed, requiredSpeedRange) {
  let range = maxSpeed - minSpeed;
  if (range > requiredSpeedRange) {
    return true;
    } else {
    return false;
  }
};

export default meetsSpeedRangeRequirements;

// missionControl.js
// Import Named Imports

import { availableAirplanes, flightRequirements, meetsStaffRequirements} from './airplane';

import meetsSpeedRangeRequirements from './airplane';

function displayFuelCapacity() {
  availableAirplanes.forEach(function(element) {
    console.log('Fuel Capacity of ' + element.name + ': ' + element['fuelCapacity']);
  });
}

displayFuelCapacity();

function displayStaffStatus() {
  availableAirplanes.forEach(function(element) {
   console.log(element.name + ' meets staff requirements: ' + meetsStaffRequirements(element.availableStaff, flightRequirements.requiredStaff) );
  });
}

displayStaffStatus();

function displaySpeedRangeStatus() {
  availableAirplanes.forEach(function(element) {
   console.log(element.name + ' meets speed range requirements:' + meetsSpeedRangeRequirements(element.maxSpeed, element.minSpeed, flightRequirements.requiredSpeedRange));
  });
}

displaySpeedRangeStatus();

Modules in JavaScript are reusable pieces of code that can be exported from one program and imported for use in another program.

  • module.exports – exports the module for use in another program.
  • require() – imports the module for use in the current program.

ES6 introduced a more flexible, easier syntax to export modules:

  • default exports use export default to export JavaScript objects, functions, and primitive data types.
  • named exports use the export keyword to export data in variables.
  • named exports can be aliased with the as keyword.
  • import is a keyword that imports any object, function, or data type.
  • Advertisements

Javascript Inheritance

Tags

class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  static generatePassword() {
    return Math.floor(Math.random() * 10000);
  }

  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}

class Nurse extends HospitalEmployee {
  constructor(name, certifications) {
    super(name);
    this._certifications = certifications;
  } 
  
  get certifications() {
    return this._certifications;
  }
  
  addCertification(newCertification) {
    this._certifications.push(newCertification);
  }
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk.remainingVacationDays);
nurseOlynyk.addCertification('Genetics');
console.log(nurseOlynyk.certifications);
  • Inheritance is when we create a parent class with properties and methods that we can extend to child classes.
  • We use the extends keyword to create a subclass.
  • The super keyword calls the constructor() of a parent class.
  • Static methods are called on the class, but not on instances of the class.

References: https://www.codecademy.com

Javascript classes

Tags

class Surgeon {
  constructor(name, department) {
    this._name = name;
    this._department = department;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get department() {
    return this._department;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}

const surgeonCurry = new Surgeon('Curry', 'Cardiovascular');
const surgeonDurant = new Surgeon('Durant', 'Orthopedics');

console.log(surgeonCurry.name);
surgeonCurry.takeVacationDays(3);
console.log(surgeonCurry.remainingVacationDays);
  • Classes are templates for objects.
  • Javascript calls a constructor method when we create a new instance of a class.

PostgreSQL: How to create user in PostgreSQL

Tags

root@27a0239d18d7:/# psql -U postgres
postgres=# CREATE USER johndoe WITH PASSWORD ‘123456’;
postgres=# ALTER USER johndoe WITH SUPERUSER;
postgres=# GRANT ALL PRIVILEGES ON DATABASE “yourDatabaseName” to johndoe;
Make sure that you assign the user to SUPERUSER else you will encounter an error that says PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations…

Linux/Ubuntu: How to use grep and find command

Tags

, , ,

How to use grep to find text including sub directories:
grep -rl {yoursearchstring} {pathtosearch} e.g.,

$ grep -rl "hello" /var/
/var/lib/dpkg/status-old
/var/lib/dpkg/status
/var/log/test.log:1:hello world

where:
-r or –recursive – is used to traverse also all sub-directories of /path
l or –files-with-matches – used to only print filenames of matching files, and not the matching lines (this could also improve the speed, given that grep stop reading a file at first match with this option).

Another way to search that will return the line matching files.

$ grep -Hrn "hello" /var/
/var/lib/dpkg/status-old:10:hello world
/var/lib/dpkg/status:13:hello world
/var/log/test.log:1:hello world

How to find/search a specific file from a directory:
find {directory} -iname {searchstring}
where:
iname – search by search string (case insensitive )
E.g.,

$ find rails-blog-heroku/ -iname *.bak
rails-blog-heroku/app/assets/javascripts/application.js.bak
rails-blog-heroku/app/assets/stylesheets/application.css.bak
rails-blog-heroku/app/assets/stylesheets/auth.css.scss.bak
rails-blog-heroku/app/assets/stylesheets/welcome.css.scss.bak
rails-blog-heroku/app/views/article/add.bak
rails-blog-heroku/app/views/article/edit.bak
rails-blog-heroku/app/views/devise/registrations/new.bak
rails-blog-heroku/app/views/devise/sessions/new.bak
rails-blog-heroku/app/views/layouts/application.html.erb.bak
rails-blog-heroku/Gemfile.bak

How to delete a files from find search results:
Search all the bak files and delete it.
$ find rails-blog-heroku/ -iname *.bak | xargs rm

OpenShift v3 tips

Tags

Tips on how to deploy your web app in Openshift v3
If you have a code changes then we need to build it again.
Click Builds menu => click {projectname} => click Start build button

If you got an error that says “quota limited”.
To fix that we need to stop all pods.

How to access your mysql in Openshift v3
$ oc login {endpoint} e.g. oc login https://api.starter-ca-central-1.openshift.com
$ oc project {projectname}
$ oc status -v — get the status of the project
$ oc get pods — get all the running pods
$ oc rsh {podname} e.g. oc rsh mysql-a24de
$ mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -p

How to import mysql database(schema.sql) in Openshift v3 with running MySQL datastore ?
Solution:
$ oc login {endpoint} e.g. oc login https://api.starter-ca-central-1.openshift.com
$ oc project {projectname}
$ oc get pods

NAME READY RESTARTS AGE
php-17-build 1/1 Running 0 16h
mysql-1-f4g6p 1/1 Running 0 16h

$ oc rsh {podname}
$ oc rsh mysql-1-f4g6p

So we are now inside the mysql pod…
The current directory / and /home don’t have a write permission. Luckily, the /var/tmp has a write permission.
sh-4.2 $ cd /var/tmp
sh-4.2 $ curl -k https://gist.githubusercontent.com/MyGitUsername/875f6/raw/b2833/schema.sql -o downloaded.sql
sh-4.2 $ mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -p < downloaded.sql

mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -p

Reactjs: Unexpected token <

Tags

,

Problem:
If you’ve encounter an error that says Unexpected token and it points to < character E.g:

ERROR in ./src/index.js
Module build failed: SyntaxError: C:/_react/react-post/src/index.js: 
Unexpected token (11:17)
> 11 |  ReactDOM.render(<div>Hello world</div>, document.getElementById('root'))
...

Solution:
Check your package.json if you have babel-preset-react. If none then we need to install it.
npm install –save-dev babel-preset-react
Next in your .babelrc, we need to add the “react” preset. E.g.

{
  "presets":[ "es2015", "react" ]
}