Skip to content

Commit 85c0379

Browse files
committed
Initial import from old repo.
1 parent 8b26a1f commit 85c0379

33 files changed

+4292
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
dist

.npmignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
src
2+
jest.config.js
3+
tsconfig.json
4+
tslint.json
5+
jsconfig.json
6+
.gitignore
7+
node_modules
8+
dist/tests
9+
fiddle.ts
10+
*.tgz

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Git
2+
3+
Common library and utilities to use Git in NodeJS
4+
5+
## Installation
6+
7+
```typescript
8+
yarn add @blendsdk/git --save
9+
```
10+
11+
```typescript
12+
npm install @blendsdk/git --save
13+
```
14+
15+
# Utlities
16+
17+
Check if we are on a given branch
18+
19+
```bash
20+
is_git_branch <branch name>
21+
```
22+
23+
Check if the current working git folder is clean
24+
25+
```bash
26+
is_git_clean
27+
```

jest.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
globals: {
3+
"ts-jest": {
4+
tsConfig: "tsconfig.json"
5+
}
6+
},
7+
moduleFileExtensions: ["ts", "js"],
8+
transform: {
9+
"^.+\\.(ts|tsx)$": "ts-jest"
10+
},
11+
testMatch: ["**/tests/**/*.spec.(ts)"],
12+
testEnvironment: "node"
13+
};

jsconfig.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"typeAcquisition": {
3+
"include": ["jest"]
4+
}
5+
}

package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@blendsdk/git",
3+
"version": "0.9.4",
4+
"description": "Standard utility functions to be used in TypeScript",
5+
"main": "dist/index.js",
6+
"author": "Gevik Babakhani <[email protected]>",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/blendsdk/stdlib.git"
10+
},
11+
"keywords": [
12+
"typescript",
13+
"utility",
14+
"language"
15+
],
16+
"license": "MIT",
17+
"types": "./dist/index.d.ts",
18+
"scripts": {
19+
"build": "rm -fR ./dist && tsc",
20+
"watch": "rm -fR ./dist && tsc -w ",
21+
"fiddle": "node ./dist/fiddle.js",
22+
"test": "jest --detectOpenHandles",
23+
"patch-publish": "node ./dist/utils/is_git_clean && node ./dist/utils/is_git_branch master && yarn build && yarn publish --patch --access public && git push origin master --tags"
24+
},
25+
"dependencies": {
26+
"tslib": "^1.10.0"
27+
},
28+
"devDependencies": {
29+
"@blendsdk/git": "^0.9.4",
30+
"@types/jest": "^24.0.15",
31+
"@types/node": "^12.0.12",
32+
"jest": "^24.8.0",
33+
"ts-jest": "^24.0.2",
34+
"typescript": "^3.5.2"
35+
}
36+
}

src/ID.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let _ID: number = 1000;
2+
3+
/**
4+
* Generates a new identifier.
5+
*
6+
* @export
7+
* @returns {number}
8+
*/
9+
export function ID(): number {
10+
return (a => {
11+
const r = a;
12+
return r;
13+
})(_ID++);
14+
}

src/apply.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { isNullOrUndef } from "./isNullOrUndef";
2+
3+
import { isArray, isObject } from "util";
4+
5+
import { wrapInArray } from "./wrapInArray";
6+
7+
/**
8+
* Copies keys and values from one object to another.
9+
* This function can optionally merge arrays and overwrite objects
10+
* in the target parameters from the source parameter.
11+
*
12+
* @export
13+
* @param {*} target
14+
* @param {*} source
15+
* @param {{ overwrite?: boolean, mergeArrays?: boolean }} [options]
16+
* @returns {*}
17+
*/
18+
export function apply<T>(target: any, source: any, options?: { overwrite?: boolean; mergeArrays?: boolean }): any {
19+
let key: any;
20+
const targetKeys = Object.keys(target || {}),
21+
targetHasKey = (k: string): boolean => {
22+
return targetKeys.indexOf(k) !== -1;
23+
};
24+
options = options || {
25+
mergeArrays: false,
26+
overwrite: false
27+
};
28+
29+
if (!isNullOrUndef(target) && !isNullOrUndef(source)) {
30+
if (isArray(target) && !isArray(source)) {
31+
target.push(source);
32+
} else {
33+
for (key in source) {
34+
if (key && source.hasOwnProperty(key)) {
35+
if (targetHasKey(key) && isObject(target[key])) {
36+
if (options.overwrite === true) {
37+
target[key] = source[key];
38+
} else {
39+
apply(target[key], source[key]);
40+
}
41+
} else if (targetHasKey(key) && isArray(target[key]) && options.mergeArrays === true) {
42+
target[key] = target[key].concat(wrapInArray(source[key]));
43+
} else if (targetHasKey(key) && options.overwrite === true) {
44+
target[key] = source[key];
45+
} else if (isNullOrUndef(target[key])) {
46+
target[key] = source[key];
47+
}
48+
}
49+
}
50+
}
51+
}
52+
return target as T;
53+
}

src/argumentsToArray.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { forEach } from "./forEach";
2+
3+
/**
4+
* Converts the function arguments to an array.
5+
*
6+
* @export
7+
* @param {IArguments} args
8+
* @returns {Array<any>}
9+
*/
10+
export function argumentsToArray(args: IArguments): any[] {
11+
const result: any[] = [];
12+
forEach(args, (item: any) => {
13+
result.push(item);
14+
});
15+
return result;
16+
}

src/dashedCase.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Converts a camelCase string to dashed case.
3+
* For example backgroundColor will be background-color
4+
*
5+
* @export
6+
* @param {string} value
7+
* @returns {string}
8+
*/
9+
export function dashedCase(value: string): string {
10+
return value.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
11+
}

src/debounce.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { TFunction } from "./types";
2+
3+
/**
4+
* Returns a function, that, as long as it continues to be invoked, will not
5+
* be triggered. The function will be called after it stops being called for
6+
* `delay` number of milliseconds. If `immediate` is passed, trigger the function
7+
* on the leading edge, instead of the trailing.
8+
*
9+
* @copyright This function ir originally ported from the underscore.js library and it copyrighted in MIT
10+
* @export
11+
* @param {number} delay
12+
* @param {Function} callback
13+
* @param {*} scope
14+
* @param {boolean} immediate
15+
* @returns {Function}
16+
*/
17+
// tslint:disable-next-line:no-shadowed-variable
18+
export function debounce(delay: number, callback: TFunction, scope?: any, immediate?: boolean): TFunction {
19+
let timeout: any;
20+
immediate = immediate || false;
21+
return function () {
22+
const me = this;
23+
scope = scope || me;
24+
const args = arguments;
25+
const later = () => {
26+
timeout = null;
27+
if (!immediate) {
28+
callback.apply(scope, args);
29+
}
30+
},
31+
callNow = immediate && !timeout;
32+
clearTimeout(timeout);
33+
timeout = setTimeout(later, delay);
34+
if (callNow) {
35+
callback.apply(scope, args);
36+
}
37+
};
38+
}

src/delay.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TFunction } from "./types";
2+
3+
/**
4+
* Runs the given callback after given milliseconds.
5+
* This function is a wrapper around setTimeout
6+
*
7+
* @export
8+
* @param {number} ms
9+
* @param {Function} callback
10+
* @param {*} [scope]
11+
*/
12+
export function delay(ms: number, callback: TFunction, scope?: any) {
13+
scope = scope || window;
14+
setTimeout(() => {
15+
callback.apply(scope, []);
16+
}, ms);
17+
}

src/ensureBoolean.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Ensures that the given value is a boolean
3+
*
4+
* @export
5+
* @param {*} value
6+
* @returns {boolean}
7+
*/
8+
export function ensureBoolean(value: any): boolean {
9+
return value === true ? true : false;
10+
}

src/forEach.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { isInstanceOf } from "./isInstanceOf";
2+
import { isFunction } from "./isFunction";
3+
import { isArray } from "./isArray";
4+
5+
/**
6+
* Loops though the given object (array, dictionary, NodeList, HTMLCollection) and runs
7+
* a callback on each item.
8+
* The callback loop will break when the callback function returns "false" explicitly!
9+
*
10+
* @export
11+
* @template T
12+
* @param {*} obj
13+
* @param {((item: T, index: number | string, scope: any) => any | boolean)} callback
14+
* @param {*} [scope]
15+
* @returns
16+
*/
17+
export function forEach<T>(
18+
obj: any,
19+
callback: (item: T, index: number | string, scope: any) => any | boolean,
20+
scope?: any
21+
) {
22+
let key: any;
23+
const isHTMLCollection = (elObj: any): boolean => {
24+
return (
25+
(elObj.constructor && elObj.constructor.name && elObj.constructor.name === "HTMLCollection") ||
26+
elObj.toString() === "[object HTMLCollection]"
27+
);
28+
};
29+
if (obj) {
30+
if (isFunction(obj)) {
31+
return;
32+
} else if (isArray(obj)) {
33+
// tslint:disable-next-line:no-shadowed-variable
34+
const length: number = obj.length;
35+
for (key = 0; key < length; key++) {
36+
if (callback.call(scope, obj[key], key, obj) === false) {
37+
break;
38+
}
39+
}
40+
} else if (isHTMLCollection(obj) || isInstanceOf(obj, NodeList)) {
41+
const length: number = obj.length;
42+
let el: HTMLElement;
43+
for (key = 0; key !== length; key++) {
44+
el = obj.item(key);
45+
if (callback.call(scope, el, key, obj) === false) {
46+
break;
47+
}
48+
}
49+
} else {
50+
for (key in obj) {
51+
if (obj.hasOwnProperty(key)) {
52+
if (callback.call(scope, obj[key], key, obj) === false) {
53+
break;
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}

src/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export * from "./apply";
2+
export * from "./argumentsToArray";
3+
export * from "./dashedCase";
4+
export * from "./debounce";
5+
export * from "./delay";
6+
export * from "./ensureBoolean";
7+
export * from "./forEach";
8+
export * from "./ID";
9+
export * from "./isArray";
10+
export * from "./isBoolean";
11+
export * from "./isClass";
12+
export * from "./isDate";
13+
export * from "./isFunction";
14+
export * from "./isInstanceOf";
15+
export * from "./isNullOrUndef";
16+
export * from "./isNumeric";
17+
export * from "./isObject";
18+
export * from "./isRegExp";
19+
export * from "./isString";
20+
export * from "./random";
21+
export * from "./shallowClone";
22+
export * from "./types";
23+
export * from "./wrapInArray";

src/isArray.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Tests whether the given value is an array.
3+
*
4+
* @export
5+
* @param {*} value
6+
* @returns {boolean}
7+
*/
8+
export function isArray(value: any): boolean {
9+
return Object.prototype.toString.apply(value) === "[object Array]";
10+
}

src/isBoolean.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Tests whether the given value is a boolean.
3+
*
4+
* @export
5+
* @param {*} value
6+
* @returns {boolean}
7+
*/
8+
export function isBoolean(value: any): boolean {
9+
return typeof value === "boolean";
10+
}

0 commit comments

Comments
 (0)