Coder Social home page Coder Social logo

isemail's Introduction

isEmail

It's a project setup as sample on how to work with TypeScript and Jest -- it contains a simple method that checks for a valid email.

It is also deployed to NPM as to demostrate the deployment process.

You will need to have nodejs and an updated version of npm.

To update to the latest NPM version run this command: npm i -g npm

1 Create a repo in github.com

Once you created the empty repo clone it:

$git clone https://github.com/<new-repo-nane>.git

$cd <new-repo-name>

2 Initialize the project

$$ npm init

3 Install TypeScript

npm i -D typescript

  • add the tsconfig.json file
{
 "compilerOptions": {
   "target": "es5",        /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
   "module": "commonjs",   /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
   "declaration": true,    /* Generates corresponding '.d.ts' file. */
   "outDir": "./dist",     /* Redirect output structure to the directory. */
   "strict": true          /* Enable all strict type-checking options. */
 }
}
  • update the package.json file
  "scripts": {
   "build": "tsc"
 }

4 Install Jest

This setup should allow you to write Jest tests in Typescript and be able to locate errors.

npm i -D jest ts-jest @types/jest

  • update the package.json
   "scripts": {
   "build": "tsc",
   "test": "jest",
   "test:watch": "jest --watch",
   "coverage": "jest --coverage"
 }
  "jest": {
   "transform": {
     "^.+\\.tsx?$": "ts-jest"
   },
   "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
   "moduleFileExtensions": [
     "ts",
     "tsx",
     "js",
     "jsx",
     "json",
     "node"
   ]
 }

5 create the source and test file

At this point create a /lib folder and the index.ts file:

export default function isEmail(str:string):boolean {
    // Verify : https://regex101.com/r/VBGyXV/1
    const re = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
    return re.test(str)

}

and the test file:

import isEmail from './index'

describe('Email Utility', ()=> {
    test('checks for a valid email', ()=> {
        expect(isEmail('[email protected]')).toBe(true)
    })
    test('recognizes a malformed email', ()=>{
        expect(isEmail('me@mail')).toBe(false)
    })
})

Setup to Publish to NPM

isemail's People

Contributors

fitfab avatar renovate[bot] avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.