Skip to content

Commit 9014b9c

Browse files
committed
add array sort snippets
1 parent 6c7d8d7 commit 9014b9c

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ Fixed for any bug fixes.
99
Security to invite users to upgrade in case of vulnerabilities.
1010
-->
1111

12+
## 3.4.0 - 2023/04/26
13+
14+
### Added
15+
16+
- array sort snippets for string, boolean, date and objects
17+
18+
### Changed
19+
20+
- `toSorted` snippets were merged with `sort`
21+
1222
## 3.3.0 - 2023/04/15
1323

1424
### Added

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ Curly brackets aren't required if only one expression is present.
8181

8282
Arrow functions do not have an arguments binding. But the same functionality can be achieved using rest parameters.
8383

84+
```js
85+
const myFunction = (...args) => {
86+
console.log(args);
87+
};
88+
myFunction(1, 2, 3); // Output: [1, 2, 3]
89+
```
90+
8491
### Use of this keyword
8592

8693
Unlike regular functions, arrow functions do not have their own `this`. The value of `this` inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of `this` in the closest non-arrow parent function.
@@ -156,10 +163,15 @@ Below is a list of all available snippets and the triggers of each one. The `░
156163
| `arfne→` | filter not equal | `const ░newArray = ░array.filter((░element) => ░element !== ░value)█` |
157164
| `arfoeq→` | filter object equal | `const ░newArray = ░array.filter((░element) => ░element.░prop === ░value)█` |
158165
| `arfone→` | filter object not equal | `const ░newArray = ░array.filter((░element) => ░element.░prop !== ░value)█` |
159-
| `arsna→` | sort (mutate) number ascending | `░array.sort((a, z) => a - z)█` |
160-
| `arsnd→` | sort (mutate) number descending | `░array.sort((a, z) => z - a)█` |
161-
| `artsna→` | sort (not mutate) number ascending | `░array.toSorted((a, z) => a - z)█` |
162-
| `artsnd→` | sort (not mutate) number descending | `░array.toSorted((a, z) => z - a)█` |
166+
| `arssa→` | sort string ascending | `░array.░sort((a, z) => a.localeCompare(z))█` |
167+
| `arssd→` | sort string descending | `░array.░sort((a, z) => z.localeCompare(a))█` |
168+
| `arsna→` | sort number ascending | `░array.░sort((a, z) => a - z)█` |
169+
| `arsnd→` | sort number descending | `░array.░sort((a, z) => z - a)█` |
170+
| `arsba→` | sort boolean ascending | `░array.░sort((a, z) => Boolean(a) - Boolean(z))█` |
171+
| `arsbd→` | sort boolean descending | `░array.░sort((a, z) => Boolean(z) - Boolean(a))█` |
172+
| `arsda→` | sort date ascending | `░array.░sort((a, z) => new Date(a) - new Date(z))█` |
173+
| `arsdd→` | sort date descending | `░array.░sort((a, z) => new Date(z) - new Date(a))c` |
174+
| `arso→` | sort object by properties | <code>░array.░sort((a, z) => {<br/>&nbsp;&nbsp;const sort = {<br/>&nbsp;&nbsp;&nbsp;&nbsp;░propString: a.░propString.localeCompare(z.░propString),<br/>&nbsp;&nbsp;&nbsp;&nbsp;░propNumber: a.░propNumber - z.░propNumber,<br/>&nbsp;&nbsp;&nbsp;&nbsp;░propBoolean: Boolean(a.░propBoolean) - Boolean(z.░propBoolean),<br/>&nbsp;&nbsp;&nbsp;&nbsp;░propDate: new Date(a.░propDate) - new Date(z.░propDate),<br/>&nbsp;&nbsp;}<br/><br/>&nbsp;&nbsp;return sort.░propString &#124;&#124; -sort.░propNumber &#124;&#124; sort.░propBoolean &#124;&#124; sort.░propDate<br>})█</code> |
163175
| `aruv→` | unique values | `const ░newArray = ░array.filter((░current, ░index, ░arr) => ░arr.indexOf(░current) == ░index)█` |
164176

165177
### Functions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "arrow-function-snippets",
33
"description": "VS Code Arrow function snippets for JS and TS",
4-
"version": "3.3.0",
4+
"version": "3.4.0",
55
"displayName": "Arrow Function Snippets",
66
"publisher": "deinsoftware",
77
"icon": "images/light-icon.png",

snippets/arrays.json

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,61 @@
1919
"body": "const ${1:newArray} = ${2:array}.filter((${3:element}) => ${3:element}.${4:prop} !== ${5:value})$0",
2020
"description": "Array Filter Object compare not equal"
2121
},
22+
"array.Sort.String.Asc": {
23+
"prefix": "arssa",
24+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => a.localeCompare(z))$0",
25+
"description": "Array sort string ascending"
26+
},
27+
"array.Sort.String.Desc": {
28+
"prefix": "arssd",
29+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => z.localeCompare(a))$0",
30+
"description": "Array sort string descending"
31+
},
2232
"array.Sort.Number.Asc": {
2333
"prefix": "arsna",
24-
"body": "${1:array}.sort((${2:a}, ${3:z}) => ${2:a} - ${3:z})$0",
25-
"description": "Array sort (mutate) numbers ascending"
34+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => a - z)$0",
35+
"description": "Array sort numbers ascending"
2636
},
2737
"array.Sort.Number.Desc": {
2838
"prefix": "arsnd",
29-
"body": "${1:array}.sort((${2:a}, ${3:z}) => ${3:z} - ${2:a})$0",
30-
"description": "Array sort (mutate) numbers descending"
31-
},
32-
"array.ToSorted.Number.Asc": {
33-
"prefix": "artsna",
34-
"body": "${1:array}.toSorted((${2:a}, ${3:z}) => ${2:a} - ${3:z})$0",
35-
"description": "Array sort (not mutate) numbers ascending"
36-
},
37-
"array.ToSorted.Number.Desc": {
38-
"prefix": "artsnd",
39-
"body": "${1:array}.toSorted((${2:a}, ${3:z}) => ${3:z} - ${2:a})$0",
40-
"description": "Array sort (not mutate) numbers descending"
39+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => z - a)$0",
40+
"description": "Array sort numbers descending"
41+
},
42+
"array.Sort.Boolean.Asc": {
43+
"prefix": "arsba",
44+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => Boolean(a) - Boolean(z))$0",
45+
"description": "Array sort boolean ascending"
46+
},
47+
"array.Sort.Boolean.Desc": {
48+
"prefix": "arsbd",
49+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => Boolean(z) - Boolean(a))$0",
50+
"description": "Array sort boolean descending"
51+
},
52+
"array.Sort.Date.Asc": {
53+
"prefix": "arsda",
54+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => new Date(a) - new Date(z))$0",
55+
"description": "Array sort date ascending"
56+
},
57+
"array.Sort.Date.Desc": {
58+
"prefix": "arsdd",
59+
"body": "${1:array}.${2|sort,toSorted|}((a, z) => new Date(z) - newDate(a))$0",
60+
"description": "Array sort date descending"
61+
},
62+
"array.Sort.Object": {
63+
"prefix": "arso",
64+
"body": [
65+
"${1:array}.${2|sort,toSorted|}((a, z) => {",
66+
"\tconst sort = {",
67+
"\t\t${3:propString}: a.${3:propString}.localeCompare(z.${3:propString}),",
68+
"\t\t${4:propNumber}: a.${4:propNumber} - z.${4:propNumber},",
69+
"\t\t${5:propBoolean}: Boolean(a.${5:propBoolean}) - Boolean(z.${5:propBoolean}),",
70+
"\t\t${6:propDate}: new Date(a.${6:propDate}) - new Date(z.${6:propDate}),",
71+
"\t}",
72+
"\t",
73+
"\treturn sort.${3:propString} || -sort.${4:propNumber} || sort.${5:propBoolean} || sort.${6:propDate}",
74+
"})$0"
75+
],
76+
"description": "Array sort object by properties"
4177
},
4278
"array.UniqueValues": {
4379
"prefix": "aruv",

0 commit comments

Comments
 (0)