Skip to content

Commit bb28a35

Browse files
committed
added formatDate
1 parent 7ad3517 commit bb28a35

17 files changed

+1498
-808
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"deno.enable": true
2+
"deno.enable": false
33
}

docs/arrayToCSV.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ Use `Array.prototype.join('\n')` to combine all rows into a CSV string, separati
1414
Omit the second argument, `delimiter`, to use a default delimiter of `,`.
1515

1616
```ts title="typescript"
17-
type StringOrNumber = string | number;
18-
const arrayToCSV = (arr: StringOrNumber[][], delimiter = ",") =>
17+
const arrayToCSV = (arr: (string | number)[][], delimiter = ",") =>
1918
arr
2019
.map((v) =>
2120
v

docs/attempt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Attempts to invoke a function with the provided arguments, returning either the
1212
Use a `try... catch` block to return either the result of the function or an appropriate error.
1313

1414
```ts title="typescript"
15-
const attempt = (fn, ...args) => {
15+
const attempt = (fn: (...args: any[]) => any, ...args: any[]) => {
1616
try {
1717
return fn(...args);
1818
} catch (e) {

docs/contains.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: contains
3+
tags: function,intermediate
4+
---
5+
6+
![TS](https://img.shields.io/badge/supports-typescript-blue.svg?style=flat-square)
7+
![JS](https://img.shields.io/badge/supports-javascript-yellow.svg?style=flat-square)
8+
![Deno](https://img.shields.io/badge/supports-deno-green.svg?style=flat-square)
9+
10+
Returns `true` if given string s1 contains s2. Compare is case insensitive.
11+
12+
```ts
13+
const contains = (s1: string, s2: string) =>
14+
s1.toLowerCase().indexOf(s2.toLowerCase());
15+
```
16+
17+
```ts
18+
contains("Text1", "ext"); // true
19+
```

docs/downloadCSV.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: downloadCSV
3+
tags: function,intermediate
4+
---
5+
6+
![TS](https://img.shields.io/badge/supports-typescript-blue.svg?style=flat-square)
7+
![JS](https://img.shields.io/badge/supports-javascript-yellow.svg?style=flat-square)
8+
![Deno](https://img.shields.io/badge/supports-deno-green.svg?style=flat-square)
9+
10+
Defers invoking a function until the current call stack has cleared.
11+
12+
Use `setTimeout()` with a timeout of 1ms to add a new event to the browser event queue and allow the rendering engine to complete its work. Use the spread (`...`) operator to supply the function with an arbitrary number of arguments.
13+
14+
```ts
15+
const downloadCSV = (csvContent: string, name: string = "download.csv") => {
16+
var encodedUri = encodeURI(csvContent);
17+
var link = document.createElement("a");
18+
link.setAttribute("href", encodedUri);
19+
link.setAttribute("download", name);
20+
document.body.appendChild(link); // Required for FF
21+
link.click(); // This will download the data file named "my_data.csv".
22+
};
23+
```
24+
25+
```ts
26+
const csvstr = arrayToCSV([
27+
["a", '"b" great'],
28+
["c", 3.1415],
29+
]); // '"a","""b"" great"\n"c",3.1415'
30+
31+
downloadCSV(csvstr, "userdetail.csv");
32+
```

docs/formatDate.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: formatDate
3+
tags: function,intermediate,date
4+
---
5+
6+
![TS](https://img.shields.io/badge/supports-typescript-blue.svg?style=flat-square)
7+
![JS](https://img.shields.io/badge/supports-javascript-yellow.svg?style=flat-square)
8+
![Deno](https://img.shields.io/badge/supports-deno-green.svg?style=flat-square)
9+
10+
Format date based on format staring, using regex match
11+
12+
```ts
13+
const padLeft = (str: string | number, num: number = 2, fill = "0") =>
14+
String(str).padStart(num, fill);
15+
16+
const formatDate = (formatStr: string, date: Date | string) => {
17+
const d = new Date(date);
18+
const time: any = {
19+
YY: padLeft(d.getFullYear()).substr(2, 4),
20+
YYYY: padLeft(d.getFullYear()),
21+
MM: padLeft(d.getMonth() + 1),
22+
DD: padLeft(d.getDate()),
23+
hh: padLeft(d.getHours()),
24+
mm: padLeft(d.getMinutes()),
25+
ss: padLeft(d.getSeconds()),
26+
M: padLeft(d.getMilliseconds(), 3),
27+
};
28+
return formatStr.replace(
29+
new RegExp(`${Object.keys(time).join("|")}`, "g"),
30+
(subStr: string) => {
31+
return time[subStr] || "";
32+
}
33+
);
34+
};
35+
```
36+
37+
```ts
38+
const date = new Date(2020, 7, 22, 22, 22, 22, 222);
39+
formatDate("YYYY-DD-MM hh:mm:ss M", date); //"2020-22-08 22:22:22 222"
40+
41+
const date2 = new Date(2020, 7, 9, 9, 9, 9, 99);
42+
formatDate("YYYY-DD-MM hh:mm:ss M", date2); // "2020-09-08 09:09:09 099"
43+
```

docs/introduction.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ All the documents can be found on [30-seconds-of-typescript](https://deepakshrma
2222
npm i @deepakvishwakarma/ts-util
2323
```
2424

25-
### How to use Typescript
25+
### How to use [Typescript]
2626

2727
```ts title="typescript"
2828
import { mask } from "@deepakvishwakarma/ts-util";
@@ -32,7 +32,7 @@ console.log(mask(1234567890, 3)); // '*******890'
3232
console.log(mask(1234567890, -4, "$")); // '$$$$567890'
3333
```
3434

35-
### How to use JS
35+
### How to use [JS]
3636

3737
```ts title="typescript"
3838
const { mask } = require("@deepakvishwakarma/ts-util");
@@ -63,7 +63,7 @@ import { all } from "https://denopkg.com/deepakshrma/30-seconds-of-typescript/ut
6363
all([{ name: "D" }, { name: "D2" }], hasName); //true
6464
```
6565

66-
## How to modern HTML|JS(Using Github CDN)
66+
## How to use using Github CDN [Modern HTML|JS]
6767

6868
```html
6969
<script type="module">

docusaurus.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
title: "30 Seconds of Typescript- Inspired by 30-seconds-of-code",
55
tagline:
66
"30 Seconds of Typescript, Code Snippets to increase productivity[WIP]",
7-
url: "https://deepakshrma.github.io/30-seconds-of-typescript/docs/",
7+
url: "https://decipher.dev",
88
baseUrl: "/30-seconds-of-typescript/",
99
favicon: "img/favicon.ico",
1010
organizationName: "deepakshrma", // Usually your GitHub org/user name.
@@ -17,6 +17,8 @@ module.exports = {
1717
logo: {
1818
alt: "30 Seconds of Typescript Logo",
1919
src: "img/logo.svg",
20+
href: "https://decipher.dev/",
21+
target: "_self",
2022
},
2123
links: [
2224
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@deepakvishwakarma/ts-util",
33
"description": "Utility library,code snippets like lodash for typescript, typescriptreact(tsx) and deno [inspired by 30-seconds-of-code].",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"private": false,
66
"scripts": {
77
"prepublishOnly": "npm test && ts-node test/test.ts",

sidebars.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = {
4747
"complement",
4848
"compose",
4949
"composeRight",
50+
"contains",
5051
"containsWhitespace",
5152
"converge",
5253
"copyToClipboard",
@@ -80,6 +81,7 @@ module.exports = {
8081
"dig",
8182
"digitize",
8283
"distance",
84+
"downloadCSV",
8385
"drop",
8486
"dropRight",
8587
"dropRightWhile",
@@ -113,6 +115,7 @@ module.exports = {
113115
"forOwnRight",
114116
"formToObject",
115117
"formatDuration",
118+
"formatDate",
116119
"frequencies",
117120
"fromCamelCase",
118121
"functionName",

0 commit comments

Comments
 (0)