@vroskus/library-helpers 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "root": true,
3
+ "parser": "@typescript-eslint/parser",
4
+ "parserOptions": {
5
+ "requireConfigFile": false,
6
+ "project": ["tsconfig.json"]
7
+ },
8
+ "extends": [
9
+ "airbnb-base",
10
+ "airbnb-typescript",
11
+ "plugin:import/recommended",
12
+ "plugin:@typescript-eslint/eslint-recommended",
13
+ "plugin:@typescript-eslint/recommended",
14
+ "plugin:sort/recommended"
15
+ ],
16
+ "plugins": [
17
+ "@typescript-eslint",
18
+ "import",
19
+ "import-newlines",
20
+ "react",
21
+ "sort"
22
+ ],
23
+ "rules": {
24
+ "complexity": ["error", 5],
25
+ "@typescript-eslint/ban-ts-comment": [1],
26
+ "@typescript-eslint/no-unused-vars": [2],
27
+ "curly": ["error", "all"],
28
+ "import/no-cycle": [0],
29
+ "no-await-in-loop": 0,
30
+ "no-console": "error",
31
+ "no-duplicate-imports": [0],
32
+ "no-loss-of-precision": [0],
33
+ "no-restricted-syntax": 0,
34
+ "no-unreachable-loop": [0],
35
+ "no-useless-backreference": [0],
36
+ "no-unused-private-class-members": [2],
37
+ "padding-line-between-statements": ["error", {
38
+ "blankLine": "always",
39
+ "prev": ["const", "let"],
40
+ "next": "*"
41
+ }, {
42
+ "blankLine": "any",
43
+ "prev": ["const", "let"],
44
+ "next": ["const", "let"]
45
+ }, {
46
+ "blankLine": "always",
47
+ "prev": ["if", "function", "for"],
48
+ "next": "*"
49
+ }, {
50
+ "blankLine": "always",
51
+ "prev": "*",
52
+ "next": "return"
53
+ }],
54
+ "object-curly-newline": ["error", {
55
+ "ObjectExpression": "always",
56
+ "ObjectPattern": "always",
57
+ "ImportDeclaration": "always",
58
+ "ExportDeclaration": "always"
59
+ }],
60
+ "function-call-argument-newline": ["error", "always"],
61
+ "object-property-newline": ["error", {
62
+ "allowMultiplePropertiesPerLine": false
63
+ }],
64
+ "import-newlines/enforce": ["error", {
65
+ "items": 1,
66
+ "forceSingleLine": false
67
+ }],
68
+ "sort/imports": [0],
69
+ "sort/exports": [0]
70
+ },
71
+ "env": {
72
+ "browser": true,
73
+ "node": true,
74
+ "jest": true,
75
+ "es6": true
76
+ }
77
+ }
@@ -0,0 +1,8 @@
1
+ export * from './common';
2
+ export declare const cleanFormValues: <D extends Record<string, unknown>>(data: Record<string, unknown>) => D;
3
+ export declare const numberValue: (input: '' | number) => number;
4
+ type $AnimationName = 'fadeIn' | 'fadeInUp' | 'fadeInDown';
5
+ export declare const animationClass: (name: $AnimationName) => string;
6
+ export declare const getUrlGetParams: (input: string) => Record<string, string>;
7
+ export declare const scrollTop: (elementId?: string, offset?: number) => void;
8
+ export declare const scrollItem: (itemId: string, elementId: string, offset?: number) => void;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AACA,oDAAuB;AACvB,gEAA8B;AAC9B,uDAI0B;AAC1B,yCAGmB;AAEnB,2CAAyB;AAEzB,MAAM,aAAa,GAAG,CAAC,KAAc,EAAW,EAAE;IAChD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE;QAC1D,OAAO,IAAI,CAAC;KACb;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEK,MAAM,eAAe,GAAG,CAC7B,IAA6B,EAC1B,EAAE;IACL,MAAM,sBAAsB,GAAG,gBAAC,CAAC,MAAM,CACrC,IAAI,EACJ,CAAC,KAAK,EAAE,GAAW,EAAE,EAAE,CAAC,gBAAC,CAAC,UAAU,CAClC,GAAG,EACH,GAAG,CACJ,CACF,CAAC;IACF,MAAM,SAAS,GAAG,gBAAC,CAAC,SAAS,CAC3B,sBAAsB,EACtB,CAA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
@@ -0,0 +1,22 @@
1
+ export declare const getValue: <V extends string | void>(value: V, defaultValue: V) => V;
2
+ export declare const durationMiddleware: <REQ extends unknown, RES extends $Response>() => (req: REQ & {
3
+ start?: [number, number];
4
+ }, res: RES, next: $Next) => void;
5
+ export declare const getDuration: (start: [number, number] | void) => number;
6
+ export declare const sortData: <D extends Record<string, unknown>[]>(data: D, field: string | Array<string>, reverse?: boolean) => Promise<D>;
7
+ export declare const getImageUrl: ({ cdnUrl, image, size, undefinedImage, }: {
8
+ cdnUrl: string;
9
+ image?: string | null;
10
+ size: 'min' | 'mid' | 'max' | 'sqr';
11
+ undefinedImage?: string;
12
+ }) => string;
13
+ export declare const getFileUrl: ({ cdnUrl, file, }: {
14
+ cdnUrl: string;
15
+ file: string;
16
+ }) => string;
17
+ export declare const makeCode: (input: string) => string;
18
+ export declare const sleep: (milliseconds: number) => Promise<void>;
19
+ export declare const isDiff: (param1: unknown, param2: unknown) => boolean;
20
+ export declare const getObjectKeys: <T extends Record<string, unknown>, K extends keyof T>(o: T) => K[];
21
+ export declare const getObjectValues: <T extends Record<string, unknown>, K extends keyof T>(o: T) => T[K][];
22
+ export declare const getObjectEntries: <T extends Record<string, unknown>, K extends keyof T>(o: T) => [K, T[K]][];
package/dist/common.js ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getObjectEntries = exports.getObjectValues = exports.getObjectKeys = exports.isDiff = exports.sleep = exports.makeCode = exports.getFileUrl = exports.getImageUrl = exports.sortData = exports.getDuration = exports.durationMiddleware = exports.getValue = void 0;
16
+ const lodash_1 = __importDefault(require("lodash"));
17
+ const aigle_1 = __importDefault(require("aigle"));
18
+ const short_hash_1 = __importDefault(require("short-hash"));
19
+ const asyncLodash = aigle_1.default.mixin(lodash_1.default, undefined);
20
+ const getValue = (value, defaultValue) => {
21
+ let output = defaultValue;
22
+ if (value !== null && value !== '') {
23
+ output = value;
24
+ }
25
+ return output;
26
+ };
27
+ exports.getValue = getValue;
28
+ const durationMiddleware = () => (req, res, next) => {
29
+ const start = process.hrtime();
30
+ req.start = start;
31
+ next();
32
+ };
33
+ exports.durationMiddleware = durationMiddleware;
34
+ const getDuration = (start) => {
35
+ let timeInMs = 0;
36
+ if (start) {
37
+ const end = process.hrtime(start);
38
+ timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
39
+ }
40
+ return timeInMs;
41
+ };
42
+ exports.getDuration = getDuration;
43
+ const sortData = (data, field, reverse) => __awaiter(void 0, void 0, void 0, function* () {
44
+ return asyncLodash
45
+ .chain(data)
46
+ .sortBy(field)
47
+ .tap(reverse ? lodash_1.default.reverse : lodash_1.default.noop)
48
+ .value();
49
+ });
50
+ exports.sortData = sortData;
51
+ const getImageUrl = ({ cdnUrl, image, size, undefinedImage, }) => {
52
+ if (image) {
53
+ return `${cdnUrl}/images/${size}/${image}`;
54
+ }
55
+ return `${cdnUrl}/static/${undefinedImage || 'dummy.png'}`;
56
+ };
57
+ exports.getImageUrl = getImageUrl;
58
+ const getFileUrl = ({ cdnUrl, file, }) => `${cdnUrl}/files/${file}`;
59
+ exports.getFileUrl = getFileUrl;
60
+ const makeCode = (input) => (0, short_hash_1.default)(input).toUpperCase();
61
+ exports.makeCode = makeCode;
62
+ const sleep = (milliseconds) => new Promise((resolve) => {
63
+ setTimeout(resolve, milliseconds);
64
+ });
65
+ exports.sleep = sleep;
66
+ const isDiff = (param1, param2) => lodash_1.default.isEqual(param1, param2) === false;
67
+ exports.isDiff = isDiff;
68
+ exports.getObjectKeys = Object.keys;
69
+ exports.getObjectValues = Object.values;
70
+ exports.getObjectEntries = Object.entries;
71
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA,oDAAuB;AACvB,kDAA0B;AAC1B,4DAA4B;AAE5B,MAAM,WAAW,GAAmB,eAAK,CAAC,KAAK,CAC7C,gBAAC,EACD,SAAS,CACV,CAAC;AAEK,MAAM,QAAQ,GAAG,CAAiC,KAAQ,EAAE,YAAe,EAAK,EAAE;IACvF,IAAI,MAAM,GAAG,YAAY,CAAC;IAE1B,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,EAAE,EAAE;QAClC,MAAM,GAAG,KAAK,CAAC;KAChB;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AARW,QAAA,QAAQ,YAQnB;AAEK,MAAM,kBAAkB,GAAG,GAEL,EAAE,CAAC,CAAC,GAEhC,EAAE,GAAQ,EAAE,IAAW,EAAE,EAAE;IACxB,MAAM,KAAK,GAAqB,OAAO,CAAC,MAAM,EAAE,CAAC;IAEjD,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC;IAElB,IAAI,EAAE,CAAC;AACT,CAAC,CAAC;AAVS,QAAA,kBAAkB,sBAU3B;AAEG,MAAM,WAAW,GAAG,CAAC,KAA8B,EAAU,EAAE;IACpE,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,IAAI,KAAK,EAAE;QACT,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAElC,QAAQ,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,UAAU,GAAG,GAAG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,MAAe,EAAW,EAAE,CAAC,gBAAC,CAAC,OAAO,CAC5E,MAAM,EACN,MAAM,CACP,KAAK,KAAK,CAAC;AAHC,QAAA,MAAM,UAGP;AAEC,QAAA,aAAa,GAEf,MAAM,CAAC,IAAI,CAAC;AAEV,QAAA,eAAe,GAEd,MAAM,CAAC,MAAM,CAAC;AAEf,QAAA,gBAAgB,GAEV,MAAM,CAAC,OAAO,CAAC"}
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,CAAC,GAAG,CAAC,0DAA0D,CAAC,CAAC"}
package/dist/node.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './common';
2
+ type $GenerateHash = (value: string) => string;
3
+ export declare const generateHash: $GenerateHash;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AACA,oDAA4B;AAE5B,2CAAyB;AAIlB,MAAM,YAAY,GAAkB,CAAC,CAAC,EAAE,EAAE,CAAC,gBAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAAtF,QAAA,YAAY,gBAA0E"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vroskus/library-helpers",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Utility helpers",
5
5
  "author": "Vilius Roškus <info@regattas.eu>",
6
6
  "license": "MIT",
@@ -9,11 +9,6 @@
9
9
  "url": "git+https://github.com/vroskus/library-helpers.git"
10
10
  },
11
11
  "main": "dist/index.js",
12
- "files": [
13
- "dist/browser.js",
14
- "dist/index.js",
15
- "dist/node.js"
16
- ],
17
12
  "scripts": {
18
13
  "postinstall": "npm run build",
19
14
  "build": "tsc",
@@ -22,6 +17,9 @@
22
17
  "test:e2e": "echo 'No tests'"
23
18
  },
24
19
  "dependencies": {
20
+ "@types/express": "4.17.17",
21
+ "@types/lodash": "4.14.192",
22
+ "@types/query-string": "6.3.0",
25
23
  "aigle": "1.14.1",
26
24
  "aphrodite": "2.4.0",
27
25
  "express": "4.18.2",
@@ -45,4 +43,4 @@
45
43
  "npm-check": "6.0.1",
46
44
  "typescript": "4.9.5"
47
45
  }
48
- }
46
+ }
package/src/browser.ts ADDED
@@ -0,0 +1,121 @@
1
+ // Helpers
2
+ import _ from 'lodash';
3
+ import qs from 'query-string';
4
+ import {
5
+ fadeIn,
6
+ fadeInDown,
7
+ fadeInUp,
8
+ } from 'react-animations';
9
+ import {
10
+ css,
11
+ StyleSheet,
12
+ } from 'aphrodite';
13
+
14
+ export * from './common';
15
+
16
+ const isEmptyString = (value: unknown): boolean => {
17
+ if (typeof value === 'string' && value.trim().length === 0) {
18
+ return true;
19
+ }
20
+
21
+ return false;
22
+ };
23
+
24
+ export const cleanFormValues = <D extends Record<string, unknown>>(
25
+ data: Record<string, unknown>,
26
+ ): D => {
27
+ const withoutHiddenItemsData = _.omitBy(
28
+ data,
29
+ (value, key: string) => _.startsWith(
30
+ key,
31
+ '_',
32
+ ),
33
+ );
34
+ const cleanData = _.mapValues(
35
+ withoutHiddenItemsData,
36
+ (value) => (value === '' || isEmptyString(value) ? null : value),
37
+ ) as D;
38
+
39
+ return cleanData;
40
+ };
41
+
42
+ export const numberValue = (input: '' | number): number => (input === '' ? 0 : input);
43
+
44
+ type $AnimationName = 'fadeIn' | 'fadeInUp' | 'fadeInDown';
45
+
46
+ export const animationClass = (name: $AnimationName): string => {
47
+ const animations: Record<$AnimationName, unknown> = {
48
+ fadeIn,
49
+ fadeInDown,
50
+ fadeInUp,
51
+ };
52
+
53
+ const style = StyleSheet.create({
54
+ animation: {
55
+ animationDuration: '1s',
56
+ animationName: animations[name],
57
+ },
58
+ });
59
+
60
+ return css(style.animation);
61
+ };
62
+
63
+ export const getUrlGetParams = (input: string): Record<string, string> => {
64
+ const parsedData = qs.parse(input);
65
+
66
+ const output = {
67
+ };
68
+
69
+ _.forEach(
70
+ parsedData,
71
+ (value: string | Array<string>, key: string) => {
72
+ if (Array.isArray(value)) {
73
+ output[key] = value.join(';');
74
+ } else {
75
+ output[key] = value;
76
+ }
77
+ },
78
+ );
79
+
80
+ return output;
81
+ };
82
+
83
+ const getScrollTopValue = (elementId?: string, offset?: number): number => {
84
+ let value = 0;
85
+
86
+ if (document && elementId) {
87
+ const element = document.getElementById(elementId);
88
+
89
+ if (element) {
90
+ value = element.offsetTop - (offset || 15);
91
+ }
92
+ }
93
+
94
+ return value;
95
+ };
96
+
97
+ export const scrollTop = (elementId?: string, offset?: number): void => {
98
+ if (window && window.scrollTo) {
99
+ window.scrollTo({
100
+ behavior: 'smooth',
101
+ top: getScrollTopValue(
102
+ elementId,
103
+ offset,
104
+ ),
105
+ });
106
+ }
107
+ };
108
+
109
+ export const scrollItem = (itemId: string, elementId: string, offset?: number): void => {
110
+ if (document) {
111
+ const item = document.getElementById(itemId);
112
+ const element = document.getElementById(elementId);
113
+
114
+ if (item && element) {
115
+ item.scroll({
116
+ behavior: 'smooth',
117
+ top: element.offsetTop + (offset || -50),
118
+ });
119
+ }
120
+ }
121
+ };
package/src/common.ts ADDED
@@ -0,0 +1,116 @@
1
+ // Global Types
2
+ import type {
3
+ NextFunction as $Next,
4
+ Request as $Request,
5
+ Response as $Response,
6
+ } from 'express';
7
+
8
+ // Helpers
9
+ import _ from 'lodash';
10
+ import aigle from 'aigle';
11
+ import sh from 'short-hash';
12
+
13
+ const asyncLodash: _.LoDashStatic = aigle.mixin(
14
+ _,
15
+ undefined,
16
+ );
17
+
18
+ export const getValue = <V extends string | null | void>(value: V, defaultValue: V): V => {
19
+ let output = defaultValue;
20
+
21
+ if (value !== null && value !== '') {
22
+ output = value;
23
+ }
24
+
25
+ return output;
26
+ };
27
+
28
+ export const durationMiddleware = <REQ extends ($Request & {
29
+ start?: [number, number];
30
+ }), RES extends $Response>() => (
31
+ req: REQ & {
32
+ start?: [number, number];
33
+ },
34
+ res: RES,
35
+ next: $Next,
36
+ ) => {
37
+ const start: [number, number] = process.hrtime();
38
+
39
+ req.start = start;
40
+
41
+ next();
42
+ };
43
+
44
+ export const getDuration = (start: [number, number] | void): number => {
45
+ let timeInMs = 0;
46
+
47
+ if (start) {
48
+ const end = process.hrtime(start);
49
+
50
+ timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
51
+ }
52
+
53
+ return timeInMs;
54
+ };
55
+
56
+ export const sortData = async <D extends Array<Record<string, unknown>>>(
57
+ data: D,
58
+ field: string | Array<string>,
59
+ reverse?: boolean,
60
+ ): Promise<D> => asyncLodash
61
+ .chain(data)
62
+ .sortBy(field)
63
+ .tap(reverse ? _.reverse : _.noop)
64
+ .value() as D;
65
+
66
+ export const getImageUrl = ({
67
+ cdnUrl,
68
+ image,
69
+ size,
70
+ undefinedImage,
71
+ }: {
72
+ cdnUrl: string;
73
+ image?: string | null;
74
+ size: 'min' | 'mid' | 'max' | 'sqr';
75
+ undefinedImage?: string;
76
+ }): string => {
77
+ if (image) {
78
+ return `${cdnUrl}/images/${size}/${image}`;
79
+ }
80
+
81
+ return `${cdnUrl}/static/${undefinedImage || 'dummy.png'}`;
82
+ };
83
+
84
+ export const getFileUrl = ({
85
+ cdnUrl,
86
+ file,
87
+ }: {
88
+ cdnUrl: string;
89
+ file: string;
90
+ }): string => `${cdnUrl}/files/${file}`;
91
+
92
+ export const makeCode = (input: string): string => sh(input).toUpperCase();
93
+
94
+ export const sleep = (milliseconds: number): Promise<void> => new Promise((resolve) => {
95
+ setTimeout(
96
+ resolve,
97
+ milliseconds,
98
+ );
99
+ });
100
+
101
+ export const isDiff = (param1: unknown, param2: unknown): boolean => _.isEqual(
102
+ param1,
103
+ param2,
104
+ ) === false;
105
+
106
+ export const getObjectKeys: <T extends Record<string, unknown>, K extends keyof T>(
107
+ o: T,
108
+ ) => K[] = Object.keys;
109
+
110
+ export const getObjectValues: <T extends Record<string, unknown>, K extends keyof T>(
111
+ o: T,
112
+ ) => T[K][] = Object.values;
113
+
114
+ export const getObjectEntries: <T extends Record<string, unknown>, K extends keyof T>(
115
+ o: T,
116
+ ) => [K, T[K]][] = Object.entries;
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line no-console
2
+ console.log('Please use platform dedicated library: /browser or /node');
package/src/node.ts ADDED
@@ -0,0 +1,8 @@
1
+ // Helpers
2
+ import crypto from 'crypto';
3
+
4
+ export * from './common';
5
+
6
+ type $GenerateHash = (value: string) => string;
7
+
8
+ export const generateHash: $GenerateHash = (v) => crypto.createHash('md5').update(v).digest('hex');
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist/",
4
+ "types": ["node", "jest"],
5
+ "module": "commonjs",
6
+ "esModuleInterop": true,
7
+ "declaration": true,
8
+ "noImplicitAny": false,
9
+ "removeComments": true,
10
+ "noLib": false,
11
+ "emitDecoratorMetadata": true,
12
+ "experimentalDecorators": true,
13
+ "target": "es6",
14
+ "sourceMap": true,
15
+ "lib": [
16
+ "dom",
17
+ "es6"
18
+ ]
19
+ },
20
+ "exclude": [
21
+ "node_modules",
22
+ "./dist/**/*"
23
+ ],
24
+ "include": [
25
+ "./src"
26
+ ]
27
+ }