Skip to content

Commit

Permalink
Version 1.3.1 (typescript hotfix).
Browse files Browse the repository at this point in the history
  • Loading branch information
igoramadas committed Feb 18, 2020
1 parent c14fb2b commit f63580d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 39 deletions.
2 changes: 1 addition & 1 deletion History.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog for JAUL

1.3.0
1.3.1
=====
* TypeScript types are now exported with the library.

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jaul",
"version": "1.3.0",
"version": "1.3.1",
"description": "Just another utilities library. But a good one.",
"keywords": [
"jaul",
Expand Down Expand Up @@ -34,7 +34,7 @@
"moment": "^2.24.0"
},
"devDependencies": {
"@types/node": "^13.7.1",
"@types/node": "^13.7.2",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"express": "^4.17.1",
Expand Down
22 changes: 14 additions & 8 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
import _ = require("lodash")

/** Data Utilities class. */
class DataUtils {
export class DataUtils {
private static _instance: DataUtils
/** @hidden */
static get Instance() {
return this._instance || (this._instance = new this())
}

/**
* Removes all the specified characters from a string. For example you can cleanup
* a phone number by using removeFromString(phone, [" ", "-", "(", ")", "/"]).
* @param value The original value to be cleaned.
* @param charsToRemove Array of characters or single string to be removed from the original string.
* @returns Value with the characters removed.
*/
static removeFromString(value: string, charsToRemove: any[] | string): string {
removeFromString = (value: string, charsToRemove: any[] | string): string => {
if (!value) {
return value
}
Expand All @@ -38,7 +44,7 @@ class DataUtils {
* @param obj Object containing the keys and values for tag replacement.
* @returns Text with tags replaced by object's values.
*/
static replaceTags = (text: string, obj: any): string => {
replaceTags = (text: string, obj: any): string => {
if (!text) {
return ""
}
Expand Down Expand Up @@ -74,7 +80,7 @@ class DataUtils {
* @param leaveLast Optional, leave last X positions of the string unmasked, default is 0.
* @returns The masked string.
*/
static maskString(value: string, maskChar?: string, leaveLast?: number): string {
maskString = (value: string, maskChar?: string, leaveLast?: number): string => {
if (!value) {
return value
}
Expand Down Expand Up @@ -124,7 +130,7 @@ class DataUtils {
* @param asString If true, return as string instead of JSON object, default is false.
* @returns The minified JSON as object or string, depending on asString.
*/
static minifyJson(source: string, asString?: boolean): any {
minifyJson = (source: string, asString?: boolean): any => {
if (_.isObject(source)) {
source = JSON.stringify(source, null, 0)
}
Expand Down Expand Up @@ -221,7 +227,7 @@ class DataUtils {
* @param value The HTML string to be converted to only text.
* @param tagReplace Replace tags with that value
*/
static stripHtml(html: string, tagReplace?: string): string {
stripHtml = (html: string, tagReplace?: string): string => {
if (!html || html == "") {
return ""
}
Expand Down Expand Up @@ -351,7 +357,7 @@ class DataUtils {
* Generates a RFC4122-compliant unique ID using random numbers.
* @returns A unique ID.
*/
static uuid(): string {
uuid = (): string => {
const baseStr = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"

const generator = function(c) {
Expand All @@ -365,4 +371,4 @@ class DataUtils {
}

// Exports...
export = DataUtils
export default DataUtils.Instance
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// JAUL: index.ts

import DataUtils = require("./data")
import IOUtils = require("./io")
import NetworkUtils = require("./network")
import SystemUtils = require("./system")
import {DataUtils} from "./data"
import {IOUtils} from "./io"
import {NetworkUtils} from "./network"
import {SystemUtils} from "./system"

/** Main JAUL class. */
class JAUL {
Expand All @@ -22,16 +22,16 @@ class JAUL {
version: string = JSON.parse(require("fs").readFileSync(`${__dirname}/../package.json`, {encoding: "utf8"})).version

/** [[DataUtils]] exposed as .data */
data: DataUtils = require("./data")
data: DataUtils = DataUtils.Instance

/** [[IOUtils]] exposed as .io */
io: IOUtils = require("./io")
io: IOUtils = IOUtils.Instance

/** [[NetworkUtils]] exposed as .network */
network: NetworkUtils = require("./network")
network: NetworkUtils = NetworkUtils.Instance

/** [[SystemUtils]] exposed as .system */
system: SystemUtils = require("./system")
system: SystemUtils = SystemUtils.Instance
}

// Exports...
Expand Down
18 changes: 12 additions & 6 deletions src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import fs = require("fs")
import path = require("path")

/** IO Utilities class. */
class IOUtils {
export class IOUtils {
private static _instance: IOUtils
/** @hidden */
static get Instance() {
return this._instance || (this._instance = new this())
}

/**
* Finds the correct path to the file looking first on the (optional) base path
* then the current or running directory, finally the root directory.
Expand All @@ -15,7 +21,7 @@ class IOUtils {
* @param basepath Optional, basepath where to look for the file.
* @returns The full path to the file if one was found, or null if not found.
*/
static getFilePath(filename: string, basepath?: string): string {
getFilePath = (filename: string, basepath?: string): string => {
const originalFilename = filename.toString()
let hasFile = false

Expand Down Expand Up @@ -60,7 +66,7 @@ class IOUtils {
* @param source The full source file path.
* @param target The full target file path.
*/
static copyFileSync(source: string, target: string): void {
copyFileSync = (source: string, target: string): void => {
const fileBuffer = fs.readFileSync(source)
fs.writeFileSync(target, fileBuffer)
}
Expand All @@ -70,7 +76,7 @@ class IOUtils {
* and creating the directories.
* @param target The full target path, with or without a trailing slash.
*/
static mkdirRecursive(target: string): void {
mkdirRecursive = (target: string): void => {
let stat

// Check if exists and not a file.
Expand Down Expand Up @@ -120,12 +126,12 @@ class IOUtils {
* @param number - How long to stall the execution for, in milliseconds.
* @returns A promise with a setTimeout for the specified milliseconds.
*/
static sleep(ms: number): Promise<Function> {
sleep = (ms: number): Promise<Function> => {
return new Promise(function(resolve) {
return setTimeout(resolve, ms)
})
}
}

// Exports...
export = IOUtils
export default IOUtils.Instance
20 changes: 13 additions & 7 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import ipaddr = require("ipaddr.js")
import os = require("os")

/** Network Utilities class. */
class NetworkUtils {
export class NetworkUtils {
private static _instance: NetworkUtils
/** @hidden */
static get Instance() {
return this._instance || (this._instance = new this())
}

/**
* Returns a list of valid server IPv4 and/or IPv6 addresses.
* @param family IP family to be retrieved, can be "IPv4" or "IPv6".
* @returns Array with the system's IP addresses, or empty.
*/
static getIP(family: string): string[] {
getIP = (family: string): string[] => {
const result = []
let ifaces = os.networkInterfaces()

Expand All @@ -37,7 +43,7 @@ class NetworkUtils {
* Returns the first valid IPv4 address found on the system, or null if no valid IPs were found.
* @returns First valid IPv4 address, or null.
*/
static getSingleIPv4(): string {
getSingleIPv4 = (): string => {
const ips = this.getIP("ipv4")

if (ips && ips.length > 0) {
Expand All @@ -52,7 +58,7 @@ class NetworkUtils {
* Returns the first valid IPv6 address found on the system, or null if no valid IPs were found.
* @returns First valid IPv6 address, or null.
*/
static getSingleIPv6(): string {
getSingleIPv6 = (): string => {
const ips = this.getIP("ipv6")

if (ips && ips.length) {
Expand All @@ -68,7 +74,7 @@ class NetworkUtils {
* @param reqOrSocket The request or socket object.
* @returns The client IP address, or null if not identified.
*/
static getClientIP(reqOrSocket: any): string {
getClientIP = (reqOrSocket: any): string => {
if (reqOrSocket == null) {
return null
}
Expand Down Expand Up @@ -114,7 +120,7 @@ class NetworkUtils {
* @param range A string or array of strings representing the valid ranges.
* @returns True if IP is in range, false otherwise.
*/
static ipInRange(ip: string, range: string[] | string): boolean {
ipInRange = (ip: string, range: string[] | string): boolean => {
if (_.isString(range)) {
const ipParsed = ipaddr.parse(ip)

Expand Down Expand Up @@ -146,4 +152,4 @@ class NetworkUtils {
}

// Exports...
export = NetworkUtils
export default NetworkUtils.Instance
14 changes: 10 additions & 4 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ interface CpuLoad {
}

/** System Utilities class. */
class SystemUtils {
export class SystemUtils {
private static _instance: SystemUtils
/** @hidden */
static get Instance() {
return this._instance || (this._instance = new this())
}

/**
* Return an object with general and health information about the system.
* @param options - Options to define the output.
* @returns Object with system metrics attached.
*/
static getInfo(options: GetInfoOptions): SystemMetrics {
getInfo = (options: GetInfoOptions): SystemMetrics => {
if (options == null) {
options = {labels: true}
}
Expand Down Expand Up @@ -104,7 +110,7 @@ class SystemUtils {
* Get current CPU load, used by getInfo().
* @returns CPU load information with idle and total counters.
*/
static getCpuLoad(): CpuLoad {
getCpuLoad = (): CpuLoad => {
let totalIdle = 0
let totalTick = 0
const cpus = os.cpus()
Expand All @@ -126,4 +132,4 @@ class SystemUtils {
}

// Exports...
export = SystemUtils
export default SystemUtils.Instance
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"target": "es2018",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"declaration": true
Expand Down

0 comments on commit f63580d

Please sign in to comment.