Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
feat: adding more helpers (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
idindrakusuma authored Jan 11, 2022
1 parent a5e369c commit 054ac73
Show file tree
Hide file tree
Showing 18 changed files with 3,732 additions and 19 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// eslint-disable-next-line no-undef
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Dependency directories
node_modules/
built/
dist/
dist/
package-lock.json
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invitato/helpers",
"version": "0.0.3",
"version": "0.0.4",
"description": "Common helpers for Invitato website projects",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -31,10 +31,18 @@
"homepage": "https://github.com/Invitato/helpers#readme",
"devDependencies": {
"@types/jest": "^27.4.0",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"eslint": "^8.6.0",
"jest": "^27.4.7",
"ts-jest": "^27.1.2",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"files": ["dist"]
"files": [
"dist"
],
"dependencies": {
"dayjs": "^1.10.7"
}
}
16 changes: 16 additions & 0 deletions src/__tests__/getQueryStringValue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import getQueryStringValue from '../getQueryStringValue';

describe('validating `src/getQueryStringValue`..', () => {
it('should return `invitation` when given `?type=invitation`', () => {
expect(getQueryStringValue({ search: '?type=invitation' }, 'type')).toBe('invitation');
});

it('should return `indra` when given `?to=indra&type=invitation`', () => {
expect(getQueryStringValue({ search: '?to=indra&type=invitation' }, 'to')).toBe('indra');
});

it('should return empty string when given invalid qs or queryName', () => {
expect(getQueryStringValue({ search: '?type=indra' }, 'to')).toBe('');
expect(getQueryStringValue({ search: '?type=indra&to=' }, 'to')).toBe('');
});
});
7 changes: 7 additions & 0 deletions src/__tests__/noop.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import noop from '../noop';

describe('validating `src/noop`...', () => {
it('function should be called', () => {
expect(noop()).toBe(undefined);
});
});
13 changes: 13 additions & 0 deletions src/canUseDOM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Function to check is can use the DOM or not
* @returns {boolean}
*/
function canUseDOM() {
if (typeof window !== 'undefined' && window.document && window.document.createElement) {
return true;
}

return false;
}

export default canUseDOM;
68 changes: 68 additions & 0 deletions src/constants/banks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const BANK_LOGO = {
bca: {
text: 'BCA (Bank Central Asia)',
logo: `https://user-images.githubusercontent.com/10141928/137078885-aff0a5f8-8d62-4415-b0dd-34e8126e3cb3.jpg`,
},
mandiri: {
text: 'Bank Mandiri',
logo: `https://user-images.githubusercontent.com/10141928/137079644-dbccb649-2e25-48b7-ba49-e7b38b6e16f4.jpg`,
},
gopay: {
text: 'Gopay',
logo: `https://user-images.githubusercontent.com/10141928/137080671-be3cd9c2-b15b-461f-b5be-072e5087cc1a.jpg`,
},
bni: {
text: 'BNI',
logo: `https://user-images.githubusercontent.com/10141928/147829773-83849afe-2a53-437d-83c5-c7d062a47f63.png`,
},
bsi: {
text: 'BSI (Bank Syariah Indonesia)',
logo: `https://user-images.githubusercontent.com/10141928/147829845-5de9fdd2-de14-4112-8931-b46eddba5c6c.png`,
},
linkAja: {
text: 'Link Aja',
logo: `https://user-images.githubusercontent.com/10141928/147829920-da1bbc92-dd4c-451d-a946-2803010a9bd3.png`,
},
payPal: {
text: 'PayPal',
logo: `https://user-images.githubusercontent.com/10141928/147829980-cb64b93f-2969-4042-ac53-c38d299631d5.png`,
},
dana: {
text: 'DANA',
logo: `https://user-images.githubusercontent.com/10141928/137604292-a9fa143e-8d34-4da3-8e71-09ecdd365712.png`,
},
bri: {
text: 'BRI (Bank Rakyat Indonesia)',
logo: `https://user-images.githubusercontent.com/10141928/137526934-cc9b1d37-609b-478d-ad12-fc96e2c13f7c.png`,
},
jenius: {
text: 'BTPN Jenius',
logo: `https://user-images.githubusercontent.com/10141928/147830197-ff989d03-2448-4aa1-9c79-a3cff3b8fe55.png`,
},
btn: {
text: 'BTN (Bank Tabungan Negara)',
logo: `https://user-images.githubusercontent.com/10141928/147830252-a0913dc3-343c-4218-b1f1-010219630ce0.png`,
},
ovo: {
text: 'OVO',
logo: `https://user-images.githubusercontent.com/10141928/148795208-627a6c1d-7bc3-430e-a69d-c15db4f785c5.png`,
},
shopeePay: {
text: 'Shopee Pay',
logo: `https://user-images.githubusercontent.com/10141928/148951310-73273d54-524a-4e13-bb4c-100a0e5d61d6.png`,
},
bankJateng: {
text: 'Bank Jateng',
logo: `https://user-images.githubusercontent.com/10141928/148952015-dd874328-69f6-4314-8714-2c9e2ff9e572.png`,
},
cimb: {
text: 'Bank CIMB',
logo: `https://user-images.githubusercontent.com/10141928/148952304-a5b6b79f-8a3d-4af5-9a3f-8b3b0e92c0d3.png`,
},
bankPermata: {
text: 'Bank Permata',
logo: `https://user-images.githubusercontent.com/10141928/148952398-e26efa80-55da-486d-aaa3-8fe01318fe76.png`,
},
};

export default BANK_LOGO;
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as BANK_LOGO from './banks';
58 changes: 58 additions & 0 deletions src/copyTextToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Fallback function to copy text if navigator clipboard not ready
* @param {Anything} text
* @returns {boolean}
*/
function fallbackCopyTextToClipboard(text: string): boolean {
const textArea = document.createElement('textarea');
let result = false;
textArea.value = text;

// Avoid scrolling to bottom
textArea.style.top = '0';
textArea.style.left = '0';
textArea.style.position = 'fixed';

document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
const successful = document.execCommand('copy');
result = successful ? true : false;
} catch (err) {
result = false;
}

document.body.removeChild(textArea);
return result;
}

/**
* Async function to copy text
* @param {Anything} text
* @returns {boolean}
*/
async function copyTextToClipboard(text: string): Promise<boolean> {
let result = false;

if (!navigator.clipboard) {
result = fallbackCopyTextToClipboard(text);
return result;
}

await navigator.clipboard.writeText(text).then(
function () {
result = true;
console.log('[SUCCESS] text copied to clipboard ✅');
},
function (err) {
console.log('[FAILED] text failed to clipboard ❌', err);
result = false;
},
);

return result;
}

export default copyTextToClipboard;
2 changes: 1 addition & 1 deletion src/getDeviceOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type OSResult = 'android' | 'ios' | 'unknown';
* @returns {OSResult}
*/
function getMobileOperatingSystem(): OSResult {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
const userAgent = navigator.userAgent || navigator.vendor || window.opera;

if (/android/i.test(userAgent)) {
return 'android';
Expand Down
15 changes: 15 additions & 0 deletions src/getQueryStringValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Function to return value from querystring
* @param {Location} location
* @param {string} name
* @returns {string}
*/
function getQueryValue(location: Pick<Location, 'search'>, queryName: string): string {
const testedName = queryName.replace(/[[]/, '\\[').replace(/[\]]/, '\\]');
const regex = new RegExp(`[\\?&]${testedName}=([^&#]*)`);
const results = regex.exec(location.search);

return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

export default getQueryValue || '';
13 changes: 13 additions & 0 deletions src/getTimeNow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dayjs from 'dayjs';
/**
* Function to get current time with default format `MM/DD/YYYY HH:mm:ss`
* For format, you can refer the `dayjs` docs
*
* @param {string} format
* @returns {string}
*/
function getTimeNow(format = 'MM/DD/YYYY HH:mm:ss'): string {
return `${dayjs(new Date()).format(format)}`;
}

export default getTimeNow;
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import getDeviceOS from './getDeviceOS';
import isDarkmode from './isDarkmode';

export { getDeviceOS, isDarkmode };
export * as canUseDOM from './canUseDOM';
export * as copyTextToClipboard from './copyTextToClipboard';
export * as getDeviceOS from './getDeviceOS';
export * as getQueryStringValue from './getQueryStringValue';
export * as getTimeNow from './getTimeNow';
export * as isDarkmode from './isDarkmode';
export * as isDevelopmentMode from './isDevelopmentMode';
export * as noop from './noop';
17 changes: 17 additions & 0 deletions src/isDevelopmentMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Function to check is Development Mode or Not
* @return {boolean}
*/
function isDevelopment(): boolean {
try {
if (typeof window.location !== 'undefined') {
if (window.location.href.includes('//localhost')) return true;
}
} catch (error) {
console.log('ERR_WHEN_ACCESS_DOM');
}

return false;
}

export default isDevelopment;
6 changes: 6 additions & 0 deletions src/noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Function to return nothing
* @returns {void}
*/
const noop = (): void => {};
export default noop;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"esModuleInterop": true
},
"include": ["./src/**/*"],
"exclude": ["node_modules", "**/__tests__/**", "dist"],
"exclude": ["node_modules", "**/__tests__/**", "**/__mocks_data__/**", "dist"],
"types": ["jest", "node"]
}
Loading

0 comments on commit 054ac73

Please sign in to comment.