Skip to content

Commit

Permalink
add array sort snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
equiman committed Apr 27, 2023
1 parent 6c7d8d7 commit 9014b9c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ Fixed for any bug fixes.
Security to invite users to upgrade in case of vulnerabilities.
-->

## 3.4.0 - 2023/04/26

### Added

- array sort snippets for string, boolean, date and objects

### Changed

- `toSorted` snippets were merged with `sort`

## 3.3.0 - 2023/04/15

### Added
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ Curly brackets aren't required if only one expression is present.

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

```js
const myFunction = (...args) => {
console.log(args);
};
myFunction(1, 2, 3); // Output: [1, 2, 3]
```

### Use of this keyword

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.
Expand Down Expand Up @@ -156,10 +163,15 @@ Below is a list of all available snippets and the triggers of each one. The `░
| `arfne→` | filter not equal | `const ░newArray = ░array.filter((░element) => ░element !== ░value)█` |
| `arfoeq→` | filter object equal | `const ░newArray = ░array.filter((░element) => ░element.░prop === ░value)█` |
| `arfone→` | filter object not equal | `const ░newArray = ░array.filter((░element) => ░element.░prop !== ░value)█` |
| `arsna→` | sort (mutate) number ascending | `░array.sort((a, z) => a - z)█` |
| `arsnd→` | sort (mutate) number descending | `░array.sort((a, z) => z - a)█` |
| `artsna→` | sort (not mutate) number ascending | `░array.toSorted((a, z) => a - z)█` |
| `artsnd→` | sort (not mutate) number descending | `░array.toSorted((a, z) => z - a)█` |
| `arssa→` | sort string ascending | `░array.░sort((a, z) => a.localeCompare(z))█` |
| `arssd→` | sort string descending | `░array.░sort((a, z) => z.localeCompare(a))█` |
| `arsna→` | sort number ascending | `░array.░sort((a, z) => a - z)█` |
| `arsnd→` | sort number descending | `░array.░sort((a, z) => z - a)█` |
| `arsba→` | sort boolean ascending | `░array.░sort((a, z) => Boolean(a) - Boolean(z))█` |
| `arsbd→` | sort boolean descending | `░array.░sort((a, z) => Boolean(z) - Boolean(a))█` |
| `arsda→` | sort date ascending | `░array.░sort((a, z) => new Date(a) - new Date(z))█` |
| `arsdd→` | sort date descending | `░array.░sort((a, z) => new Date(z) - new Date(a))c` |
| `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> |
| `aruv→` | unique values | `const ░newArray = ░array.filter((░current, ░index, ░arr) => ░arr.indexOf(░current) == ░index)█` |

### Functions
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "arrow-function-snippets",
"description": "VS Code Arrow function snippets for JS and TS",
"version": "3.3.0",
"version": "3.4.0",
"displayName": "Arrow Function Snippets",
"publisher": "deinsoftware",
"icon": "images/light-icon.png",
Expand Down
64 changes: 50 additions & 14 deletions snippets/arrays.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,61 @@
"body": "const ${1:newArray} = ${2:array}.filter((${3:element}) => ${3:element}.${4:prop} !== ${5:value})$0",
"description": "Array Filter Object compare not equal"
},
"array.Sort.String.Asc": {
"prefix": "arssa",
"body": "${1:array}.${2|sort,toSorted|}((a, z) => a.localeCompare(z))$0",
"description": "Array sort string ascending"
},
"array.Sort.String.Desc": {
"prefix": "arssd",
"body": "${1:array}.${2|sort,toSorted|}((a, z) => z.localeCompare(a))$0",
"description": "Array sort string descending"
},
"array.Sort.Number.Asc": {
"prefix": "arsna",
"body": "${1:array}.sort((${2:a}, ${3:z}) => ${2:a} - ${3:z})$0",
"description": "Array sort (mutate) numbers ascending"
"body": "${1:array}.${2|sort,toSorted|}((a, z) => a - z)$0",
"description": "Array sort numbers ascending"
},
"array.Sort.Number.Desc": {
"prefix": "arsnd",
"body": "${1:array}.sort((${2:a}, ${3:z}) => ${3:z} - ${2:a})$0",
"description": "Array sort (mutate) numbers descending"
},
"array.ToSorted.Number.Asc": {
"prefix": "artsna",
"body": "${1:array}.toSorted((${2:a}, ${3:z}) => ${2:a} - ${3:z})$0",
"description": "Array sort (not mutate) numbers ascending"
},
"array.ToSorted.Number.Desc": {
"prefix": "artsnd",
"body": "${1:array}.toSorted((${2:a}, ${3:z}) => ${3:z} - ${2:a})$0",
"description": "Array sort (not mutate) numbers descending"
"body": "${1:array}.${2|sort,toSorted|}((a, z) => z - a)$0",
"description": "Array sort numbers descending"
},
"array.Sort.Boolean.Asc": {
"prefix": "arsba",
"body": "${1:array}.${2|sort,toSorted|}((a, z) => Boolean(a) - Boolean(z))$0",
"description": "Array sort boolean ascending"
},
"array.Sort.Boolean.Desc": {
"prefix": "arsbd",
"body": "${1:array}.${2|sort,toSorted|}((a, z) => Boolean(z) - Boolean(a))$0",
"description": "Array sort boolean descending"
},
"array.Sort.Date.Asc": {
"prefix": "arsda",
"body": "${1:array}.${2|sort,toSorted|}((a, z) => new Date(a) - new Date(z))$0",
"description": "Array sort date ascending"
},
"array.Sort.Date.Desc": {
"prefix": "arsdd",
"body": "${1:array}.${2|sort,toSorted|}((a, z) => new Date(z) - newDate(a))$0",
"description": "Array sort date descending"
},
"array.Sort.Object": {
"prefix": "arso",
"body": [
"${1:array}.${2|sort,toSorted|}((a, z) => {",
"\tconst sort = {",
"\t\t${3:propString}: a.${3:propString}.localeCompare(z.${3:propString}),",
"\t\t${4:propNumber}: a.${4:propNumber} - z.${4:propNumber},",
"\t\t${5:propBoolean}: Boolean(a.${5:propBoolean}) - Boolean(z.${5:propBoolean}),",
"\t\t${6:propDate}: new Date(a.${6:propDate}) - new Date(z.${6:propDate}),",
"\t}",
"\t",
"\treturn sort.${3:propString} || -sort.${4:propNumber} || sort.${5:propBoolean} || sort.${6:propDate}",
"})$0"
],
"description": "Array sort object by properties"
},
"array.UniqueValues": {
"prefix": "aruv",
Expand Down

0 comments on commit 9014b9c

Please sign in to comment.