Skip to content
This repository was archived by the owner on Jan 12, 2023. It is now read-only.

Commit cef5a52

Browse files
author
Kevin Hermawan
committed
first commit
0 parents  commit cef5a52

28 files changed

+8738
-0
lines changed

.eslintrc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true,
5+
node: true,
6+
},
7+
extends: [
8+
'eslint:recommended',
9+
'plugin:@typescript-eslint/recommended',
10+
'plugin:prettier/recommended',
11+
],
12+
parser: '@typescript-eslint/parser',
13+
parserOptions: {
14+
ecmaVersion: 'latest',
15+
sourceType: 'module',
16+
project: './tsconfig.json',
17+
},
18+
plugins: ['prettier', '@typescript-eslint'],
19+
rules: {},
20+
};

.github/workflows/code-quality.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Code Quality
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
matrix:
17+
node-version: [16.x, 18.x]
18+
19+
steps:
20+
- uses: actions/checkout@v2
21+
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v2
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
27+
- name: Install dependencies
28+
run: npm install
29+
30+
- name: Run lint
31+
run: npm run lint
32+
33+
- name: Run unit test
34+
run: npm run test:coverage
35+
36+
build:
37+
runs-on: ubuntu-latest
38+
39+
strategy:
40+
matrix:
41+
node-version: [16.x, 18.x]
42+
43+
steps:
44+
- uses: actions/checkout@v2
45+
46+
- name: Use Node.js ${{ matrix.node-version }}
47+
uses: actions/setup-node@v2
48+
with:
49+
node-version: ${{ matrix.node-version }}
50+
51+
- name: Install dependencies
52+
run: npm install
53+
54+
- name: Build package
55+
run: npm run build

.github/workflows/release.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
release:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
14+
- name: Use Node.js
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: '16.x'
18+
registry-url: https://registry.npmjs.org
19+
20+
- name: Install dependencies
21+
run: npm install
22+
23+
- name: Build package
24+
run: npm run build
25+
26+
- name: Release package
27+
run: npm publish --verbose --access public
28+
env:
29+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
coverage
12+
dist
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npx commitlint --edit

.prettierrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
semi: true,
3+
singleQuote: true,
4+
bracketSameLine: false,
5+
trailingComma: 'es5',
6+
};

README.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Enhanced Array
2+
3+
A collection of common utilities for the JavaScript array.
4+
5+
> This library copies common utilities from other languages. For advanced utilities, please use a library like Lodash instead.
6+
7+
## Installation
8+
9+
```
10+
npm i enhanced-array
11+
```
12+
13+
Only need a few? Just copy the source code :)
14+
15+
## API
16+
17+
### Accessing Elements
18+
19+
#### [getFirst](/src/getFirst.ts)
20+
21+
Returns the first element of the array.
22+
23+
```ts
24+
import { getFirst } from 'enhanced-array';
25+
26+
const result = getFirst([1, 2, 3, 4, 5]);
27+
console.log(result); // 1
28+
```
29+
30+
> **Time complexity:**
31+
>
32+
> O(1)
33+
34+
#### [getLast](/src/getLast.ts)
35+
36+
Returns the last element of the array.
37+
38+
```ts
39+
import { getLast } from 'enhanced-array';
40+
41+
const result = getLast([1, 2, 3, 4, 5]);
42+
console.log(result); // 5
43+
```
44+
45+
> **Time complexity:**
46+
>
47+
> O(1)
48+
49+
---
50+
51+
### Adding Elements
52+
53+
#### [insertAt](/src/insertAt.ts)
54+
55+
Inserts a new element at the specified position.
56+
57+
```ts
58+
import { insertAt } from 'enhanced-array';
59+
60+
const result = insertAt([1, 2, 3, 4, 5], { index: 3, element: 9 });
61+
console.log(result); // [1, 2, 3, 9, 4, 5]
62+
```
63+
64+
> **Time complexity:**
65+
>
66+
> O(n), where n is the length of the array.
67+
>
68+
> O(1), if `index` is the last index of the array.
69+
70+
---
71+
72+
### Inspecting Elements
73+
74+
#### [isContainNil](/src/isContainNil.ts)
75+
76+
Returns a boolean that indicates whether the array contains `undefined` or `null`.
77+
78+
```ts
79+
import { isContainNil } from 'enhanced-array';
80+
81+
const result1 = isContainNil([1, 2, 3, 4, 5]);
82+
console.log(result1); // false
83+
84+
const result2 = isContainNil([1, 2, undefined, 4, 5]);
85+
console.log(result2); // true
86+
87+
const result3 = isContainNil([1, 2, null, 4, 5]);
88+
console.log(result3); // true
89+
```
90+
91+
> **Time complexity:**
92+
>
93+
> O(n), where n is the length of the array.
94+
>
95+
> O(1), if `index` is the last index of the array.
96+
97+
#### [isEmpty](/src/isEmpty.ts)
98+
99+
Returns a boolean that indicates whether the array is empty.
100+
101+
```ts
102+
import { isEmpty } from 'enhanced-array';
103+
104+
const result1 = isEmpty([1, 2, 3, 4, 5]);
105+
console.log(result1); // false
106+
107+
const result2 = isEmpty([]);
108+
console.log(result2); // true
109+
```
110+
111+
> **Time complexity:**
112+
>
113+
> O(1)
114+
115+
---
116+
117+
### Removing Elements
118+
119+
#### [removeAt](/src/removeAt.ts)
120+
121+
Removes the element at the specified position.
122+
123+
```ts
124+
import { removeAt } from 'enhanced-array';
125+
126+
const result = removeAt([1, 2, 3, 4, 5], { index: 3 });
127+
console.log(result); // [1, 2, 3, 5]
128+
```
129+
130+
> **Time complexity:**
131+
>
132+
> O(n), where n is the length of the array.
133+
>
134+
> O(1), if `index` is the last index of the array.
135+
136+
---
137+
138+
### Reordering Elements
139+
140+
#### [swap](/src/swap.ts)
141+
142+
Exchanges the element at the specified indices of the array.
143+
144+
```ts
145+
import { swap } from 'enhanced-array';
146+
147+
const result = swap([1, 2, 3, 4, 5], { i: 0, j: 4 });
148+
149+
console.log(result); // [5, 2, 3, 4, 1]
150+
```
151+
152+
> **Time complexity:**
153+
>
154+
> O(1)
155+
156+
## Stolen From
157+
158+
- [Swift's Array](https://developer.apple.com/documentation/swift/array)

commitlint.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
rules: {
4+
'subject-empty': [0],
5+
'type-enum': [
6+
2,
7+
'always',
8+
['chore', 'feat', 'fix', 'test', 'docs', 'refactor', 'revert', 'release'],
9+
],
10+
},
11+
};

0 commit comments

Comments
 (0)