arkilis
7/28/2017 - 12:52 AM

es2015module.js

////////////////////////////////////////////////////////////////
// 1. Define and export constant varaible
////////////////////////////////////////////////////////////////

const MAX_TITLE_LENGTH = 20;
const EMAIL_DOMAIN = "@codeschool.com";

export {MAX_TITLE_LENGTH, EMAIL_DOMAIN};




////////////////////////////////////////////////////////////////
// 2. Define and export functions
// There are two ways of doing so:
////////////////////////////////////////////////////////////////

// Method 1: export each function while define
// is-topic-valid.js
export function isTopicValid(topic){
  const MAX_TITLE_LENGTH = 20;

  let isValid = !(topic.title.length > MAX_TITLE_LENGTH || topic.author.isBlocked);
  return isValid;
}

export function isEmailAuthorized(email){
  const EMAIL_DOMAIN = "@codeschool.com";
  return email.indexOf(EMAIL_DOMAIN) > 0;
}

// Method 2: export functions seperately
// is-topic-valid.js
function isTopicValid(topic){
  const MAX_TITLE_LENGTH = 20;

  let isValid = !(topic.title.length > MAX_TITLE_LENGTH || topic.author.isBlocked);
  return isValid;
}

function isEmailAuthorized(email){
  const EMAIL_DOMAIN = "@codeschool.com";
  return email.indexOf(EMAIL_DOMAIN) > 0;
}

export { isTopicValid, isEmailAuthorized };


// Then import above functions in a new file. app.js
import isTopicValid from './is-topic-valid';

let topic = {
  title: "ES2015",
  author: { name: "Sam", isBlocked: false }
};

isTopicValid(topic);




////////////////////////////////////////////////////////////////
// 3. Define and export classes
////////////////////////////////////////////////////////////////
export default class TagManager {

  constructor(topicId){
    this.topicId = topicId;
  }
  
  addTag(tagName){
    API.createTag(tagName, this.topicId);
  }
  
  removeTag(tagName){
    API.deleteTag(tagName, this.topicId);
  }
}