Skip to content

Commit

Permalink
Typescript (#11)
Browse files Browse the repository at this point in the history
* initial refactoring

* typing

* update ts & more tests

* fix travis

* update README & rm files

* nycrc

* npm badge
  • Loading branch information
ettoreleandrotognoli authored and leonardortlima committed Oct 25, 2018
1 parent 46a020d commit 4ee76a2
Show file tree
Hide file tree
Showing 22 changed files with 146 additions and 3,027 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

1 change: 0 additions & 1 deletion .coveralls.yml

This file was deleted.

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
.*
!.gitignore
!.travis.yml
!.coveralls.yml
!.babelrc
!.nycrc
**.log
**.lock
**-lock.json
Expand Down
19 changes: 19 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extension": [
".ts"
],
"require": [
"ts-node/register"
],
"include": [
"src/**/*.ts"
],
"exclude": [
"**/*.d.ts"
],
"reporter": [
"html",
"text"
],
"all": true
}
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
language: node_js
node_js:
- 10
- 8
script: yarn test
after_success: yarn coverage
- 10
script: npm run test
after_success: npm run coverage
install:
- yarn
- npm install
cache:
yarn: true
directories:
- node_modules
- node_modules
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# common-lang
[![Build Status](https://travis-ci.org/HuehueJS/common-lang.svg?branch=master)](https://travis-ci.org/HuehueJS/common-lang)
[![Coverage Status](https://coveralls.io/repos/github/HuehueJS/common-lang/badge.svg?branch=master)](https://coveralls.io/github/HuehueJS/common-lang?branch=master)
[![npm version](https://badge.fury.io/js/%40huehuejs%2Fcommon-lang.svg)](https://npmjs.com/@huehuejs/common-lang)
[![npm version](https://badge.fury.io/js/%40huehuejs%2Fcommon-lang.svg)](https://npmjs.com/@huehuejs/common-lang)
[![Maintainability](https://api.codeclimate.com/v1/badges/24f83743eeb7bff31d66/maintainability)](https://codeclimate.com/github/HuehueJS/common-lang/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/24f83743eeb7bff31d66/test_coverage)](https://codeclimate.com/github/HuehueJS/common-lang/test_coverage)
44 changes: 24 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
{
"name": "@huehuejs/common-lang",
"version": "0.0.1",
"private": false,
"version": "0.1.0",
"description": "Exploring",
"main": "index.js",
"types": "index.d.ts",
"files": [
"**/*.js"
"**/*"
],
"repository": "https://github.com/HuehueJS/common-lang",
"author": "HuehueJs",
"license": "BSD",
"private": false,
"license": "MIT",
"standard": {
"ignore": [
"coverage/**",
"node_modules/**",
"src/**"
"node_modules/**"
]
},
"scripts": {
"build": "babel ./src -d ./dist",
"build-dev": "babel ./src -d ./dist --source-maps inline",
"predeploy": "rm -rf dist && mkdir -p dist && npm run build && cp ./package.json ./dist/",
"deploy": "cd ./dist && npm publish --access public",
"postdeploy": "cd ..",
"test": "nyc --reporter=html --reporter=text mocha --recursive --compilers js:babel-core/register",
"coverage": "nyc report --reporter=text-lcov | coveralls"
"prebuild": "rm -rf dist",
"build": "tsc",
"postbuild": "cp package.json dist/",
"test": "nyc --reporter=html --reporter=text mocha test/**/*.spec.ts",
"predeploy": "npm run build",
"deploy": "cd dist/ && npm publish --access public",
"coverage": "nyc report --reporter=text-lcov | codeclimate-test-reporter "
},
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-es2018": "^1.0.0",
"@types/chai": "^4.1.4",
"@types/mocha": "^5.2.5",
"chai": "^4.1.2",
"coveralls": "^3.0.1",
"mocha": "^5.1.1",
"nyc": "^11.7.1"
"chai-string": "^1.4.0",
"codeclimate-test-reporter": "^0.5.0",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"ts-node": "^7.0.0",
"typescript": "^3.1.3"
},
"directories": {
"test": "test"
}
}
39 changes: 21 additions & 18 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
export const isNullOrUndefined = obj => [null, undefined].indexOf(obj) !== -1;
export const isNullOrUndefined = (obj: any) => [null, undefined].indexOf(obj) !== -1;

export const requireNotNull = function(obj, message) {
if(isNullOrUndefined(obj)) {
export const requireNotNull = function (obj: any, message: string) {
if (isNullOrUndefined(obj)) {
throw Error(message);
}
}

export const isCallable = obj => obj instanceof Function;
export const isCallable = (obj: any) => obj instanceof Function;

export const clone = (obj: any) => JSON.parse(JSON.stringify(obj));

export const clone = obj => JSON.parse(JSON.stringify(obj));
export const noop = (x: any) => x;

export const noop = x => x;
export type KeyPiece = string | number;
export type Key = Array<KeyPiece>;

export const getattr = function (obj, key, defaultValue = undefined) {
export const getattr = (obj: any, key: Key | KeyPiece, defaultValue: any = undefined) => {
if (isNullOrUndefined(obj)) {
return defaultValue;
}
if (typeof obj !== 'object') {
throw TypeError();
}
if (['string', 'number'].indexOf(typeof key) !== -1) {
key = [key];
key = [key as KeyPiece];
}
let currentValue = obj;
for (let k of key) {
for (let k of (key as Key)) {
currentValue = currentValue[k];
if (currentValue === undefined) {
return defaultValue;
Expand All @@ -32,18 +35,18 @@ export const getattr = function (obj, key, defaultValue = undefined) {
return currentValue;
}

export const setattr = function (obj, key, value = null, force = false) {
export const setattr = (obj: any, key: Key | KeyPiece, value: any = null, force: boolean = false) => {
if (isNullOrUndefined(obj)) {
throw TypeError();
}
if (typeof obj !== 'object') {
throw TypeError();
}
if (['string', 'number'].indexOf(typeof key) !== -1) {
key = [key];
key = [key as KeyPiece];
}
let currentValue = obj;
for (let k of key.slice(0, -1)) {
for (let k of (key as Key).slice(0, -1)) {
if (!(k in currentValue)) {
currentValue[k] = {}
}
Expand All @@ -52,20 +55,20 @@ export const setattr = function (obj, key, value = null, force = false) {
}
currentValue = currentValue[k]
}
currentValue[key.slice(-1)[0]] = value;
currentValue[(key as Key).slice(-1)[0]] = value;
}

export const isEmpty = function (obj) {
export const isEmpty = (obj: any) => {
if (isNullOrUndefined(obj)) { return true; }
if(typeof obj === 'number') {
if (typeof obj === 'number') {
if (obj === 0) { return true; }
return false
}
if (Object.keys(obj).length === 0) { return true; }
return false;
}

export const firstNonEmpty = function (obj) {
if (!obj) return null;
return obj.find(it => !isEmpty(it));
export function firstNonEmpty<E>(obj: E | Array<E>): E | Array<E> {
if (isEmpty(obj)) return obj;
return (obj as Array<E>).find(it => !isEmpty(it));
}
13 changes: 6 additions & 7 deletions src/string/index.js → src/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { getattr, isEmpty } from '../index'

export const isString = obj => typeof obj === 'string';
export const isString = (obj : any) => typeof obj === 'string';

export const upperCaseFirst = function (string) {
export const upperCaseFirst = (string : string) => {
return string[0].toUpperCase() + string.slice(1);
}

export const lowerCaseFirst = function (string) {
export const lowerCaseFirst = (string : string) => {
return string[0].toLowerCase() + string.slice(1);
}

export const upperCase = function (string) {
export const upperCase = (string : string) => {
return string.toUpperCase();
}

export const lowerCase = function (string) {
export const lowerCase = (string : string ) => {
return string.toLowerCase();
}

export const format = function (message, data) {
export const format = (message : string, data : any) => {
if (isEmpty(data)) {
return message;
}

let formatedMessage = message;
let paramRegex = new RegExp("{([\\w\.\\(\\),]+)}", 'g');
let result = null;
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions test/first-non-empty.spec.js → test/first-non-empty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import { expect } from 'chai';
import { firstNonEmpty } from '../src/index';

describe('#firstNonEmpty', () => {
it("pass a empty [], should return the same empty []", () =>{
const empty = [];
expect(firstNonEmpty(empty)).to.equal(empty);
})
it("pass a empty null, should return the same empty null", ()=>{
const empty = null;
expect(firstNonEmpty(empty)).to.equal(empty);
})

it("pass a empty undefined, should return the same empty undefined", ()=>{
const empty = undefined;
expect(firstNonEmpty(empty)).to.equal(empty);
})
it("pass a empty 0, should return the same empty 0", ()=>{
const empty = 0;
expect(firstNonEmpty(empty)).to.equal(empty);
})
it("pass a empty {}, should return the same empty {}", ()=>{
const empty = {};
expect(firstNonEmpty(empty)).to.deep.equal(empty);
})
it("pass [1,2,null], should return 1", () => {
expect(firstNonEmpty([1,2,null])).to.equal(1);
})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions test/noop.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect } from 'chai';
import { isString } from '../src/string/index';
import { noop } from '../src';

describe("#noop", () => {
it('noop should do nothing', () => {
const anyValue = 'Tony Stark';
expect(noop(anyValue)).to.equal(anyValue);
})
})
File renamed without changes.
7 changes: 6 additions & 1 deletion test/setattr.spec.js → test/setattr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { setattr } from '../src/index'
import { expect } from 'chai'

describe("#setattr", function () {
it('when key exists should set the value', function () {
let obj = { 'key': 1 };
setattr(obj, 'key')
expect(obj.key).to.equal(null)
})
it('when key exists should set the value', function () {
let obj = { 'key': null };
setattr(obj, 'key', 'value')
Expand All @@ -13,7 +18,7 @@ describe("#setattr", function () {
expect(obj.key).to.equal('value')
})
it('when key is an array and doesn\'t exists should put the value recursively', function () {
let obj = {};
const obj : any = {};
setattr(obj, ['key_1', 'key_2'], 'value')
expect(obj.key_1.key_2).to.equal('value')
})
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compileOnSave": false,
"compilerOptions": {
"declarationDir": "./dist/",
"outDir": "./dist/",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"declaration": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"es2017.object",
"dom"
],
"plugins": [
{
"name": "tslint-language-service"
}
]
},
"include": [
"./src"
]
}
Loading

0 comments on commit 4ee76a2

Please sign in to comment.