Skip to content

Commit

Permalink
feat: add prettier config and run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Apr 29, 2021
1 parent 78b609a commit b01a22b
Show file tree
Hide file tree
Showing 61 changed files with 531 additions and 361 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: ⬆️ Upload coverage report
uses: codecov/codecov-action@v1
release:
if:
if:
${{ github.repository == 'arcath/utils' &&
contains('refs/heads/master,refs/heads/beta,refs/heads/next,refs/heads/alpha',
github.ref) && github.event_name == 'push' }}
Expand All @@ -67,13 +67,13 @@ jobs:

- name: 📚 Build Docs
run: npm run document
- name: 🚀 Deploy

- name: 🚀 Deploy
uses: JamesIves/[email protected]
with:
branch: gh-pages
folder: docs

- name: 🏗 Build
run: npm run build

Expand All @@ -92,4 +92,4 @@ jobs:
]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .husky.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = {}
module.exports = {}
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/
coverage/
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./config/prettierrc')
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"titleBar.activeBackground": "#08474E",
"titleBar.activeForeground": "#F0FCFD"
}
}
}
6 changes: 4 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Utils

[![codecov](https://codecov.io/gh/Arcath/utils/branch/master/graph/badge.svg?token=2R7fS9DBHW)](https://codecov.io/gh/Arcath/utils) [![buid status](https://img.shields.io/github/workflow/status/arcath/utils/validate?logo=github&style=flat-square)](https://github.com/arcath/utils/actions?query=workflow%3Avalidate) [![NPM](https://img.shields.io/npm/v/@arcath/utils.svg?style=flat-square)](https://www.npmjs.com/package/@arcath/utils)
[![codecov](https://codecov.io/gh/Arcath/utils/branch/master/graph/badge.svg?token=2R7fS9DBHW)](https://codecov.io/gh/Arcath/utils)
[![buid status](https://img.shields.io/github/workflow/status/arcath/utils/validate?logo=github&style=flat-square)](https://github.com/arcath/utils/actions?query=workflow%3Avalidate)
[![NPM](https://img.shields.io/npm/v/@arcath/utils.svg?style=flat-square)](https://www.npmjs.com/package/@arcath/utils)

A collection of utility functions bundled into one easy to install library.

Expand All @@ -16,4 +18,4 @@ yarn add @arcath/utils

## More

A full list of functions and classes can be found at https://utils.arcath.net
A full list of functions and classes can be found at https://utils.arcath.net
19 changes: 19 additions & 0 deletions config/prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
arrowParens: 'avoid',
bracketSpacing: false,
embeddedLanguageFormatting: 'auto',
endOfLine: 'lf',
htmlWhitespaceSensitivity: 'css',
insertPragma: false,
jsxBracketSameLine: false,
jsxSingleQuote: false,
printWidth: 80,
proseWrap: 'always',
quoteProps: 'as-needed',
requirePragma: false,
semi: false,
singleQuote: true,
tabWidth: 2,
trailingComma: 'none',
useTabs: false
}
10 changes: 5 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const configs = require('kcd-scripts/config')

module.exports = Object.assign(configs.jest, {
"testMatch": ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
"transform": {
"^.+\\.tsx?$": "ts-jest"
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
"collectCoverage": true
})
collectCoverage: true
})
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"lint": "kcd-scripts lint",
"build": "tsc",
"document": "typedoc --out ./docs --entryPoints src/index.ts --name Utils --readme Readme.md && echo utils.arcath.net > ./docs/CNAME",
"validate": "kcd-scripts validate"
"validate": "kcd-scripts validate",
"format": "kcd-scripts format"
},
"author": "Adam Laycock <[email protected]>",
"license": "MIT",
Expand All @@ -18,6 +19,7 @@
"@types/node": "^15.0.0",
"jest": "^26.6.3",
"kcd-scripts": "^10.0.0",
"prettier": "^2.2.1",
"ts-jest": "^26.5.5",
"typedoc": "^0.20.36",
"typescript": "^4.2.4"
Expand Down
21 changes: 6 additions & 15 deletions src/classes/bit-mask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@ const READ = 'read'
const WRITE = 'write'
const EXECUTE = 'execute'

type Permission =
typeof EXECUTE |
typeof READ |
typeof WRITE


const Bits: Permission[] = [
READ,
WRITE,
EXECUTE
]
type Permission = typeof EXECUTE | typeof READ | typeof WRITE

const Bits: Permission[] = [READ, WRITE, EXECUTE]

describe('BitMask', () => {
it('should set values', () => {
const mask = new BitMask(0, Bits)

expect(mask.get(READ)).toBe(false)

mask.set(READ, true)
mask.set(READ, true)

Expand Down Expand Up @@ -58,10 +49,10 @@ describe('BitMask', () => {
expect(mask.value).toBe(0)

//eslint-disable-next-line
expect((mask as any).get('test')).toBe(false);
expect((mask as any).get('test')).toBe(false)

//eslint-disable-next-line
(mask as any).set('test')
;(mask as any).set('test')
expect(mask.value).toBe(0)
})
})
})
48 changes: 24 additions & 24 deletions src/classes/bit-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
/**
* Bit Masker utility class.
*/
export class BitMask<T>{
export class BitMask<T> {
/** The current value of the mask */
value: number
bits: T[]

/**
*
*
* @param value The current value of the bit mask, defaults to `0`
* @param bits The array of names to use e.g. `['canOpen', 'canClose']`
*/
constructor(value: number = 0, bits: T[] = []){
constructor(value: number = 0, bits: T[] = []) {
this.value = value
this.bits = bits
}

_get(bit: number){
_get(bit: number) {
return !!((1 << bit) & this.value)
}

_set(bit: number, value: boolean){
if(value){
if(!this._get(bit)){
_set(bit: number, value: boolean) {
if (value) {
if (!this._get(bit)) {
this.value = (1 << bit) ^ this.value
}
}else if(this._get(bit)){
} else if (this._get(bit)) {
this.value = (1 << bit) ^ this.value
}
}

/**
* Checks the given bit name for its boolean value.
*
*
* @param name The name of the bit to check. e.g. `canOpen`
*/
get(name: T){
get(name: T) {
const index = this.bits.indexOf(name)

if(index !== -1){
if (index !== -1) {
return this._get(index)
}

Expand All @@ -49,45 +49,45 @@ export class BitMask<T>{

/**
* Sets the given bit to the supplied value.
*
*
* @param name The name of the bit to set e.g. `canClose`
* @param value The boolean value to set it to.
*/
set(name: T, value: boolean){
set(name: T, value: boolean) {
const index = this.bits.indexOf(name)
if(index !== -1){

if (index !== -1) {
this._set(index, value)
}
}

asArray(){
return this.bits.filter((bit) => {
asArray() {
return this.bits.filter(bit => {
return this.get(bit)
})
}

/**
* Returns an array of indexes in the bits array that are currently true.
*/
asIndexArray(){
return this.asArray().map((bit) => {
asIndexArray() {
return this.asArray().map(bit => {
return this.bits.indexOf(bit)
})
}

/**
* Sets all values in the mask to true if they are in the array and false if they are not.
*
*
* @param indexs An array of indexes in the bits array that need to be true.1
*/
fromIndexArray(indexs: number[]){
fromIndexArray(indexs: number[]) {
this.bits.forEach((bit, index) => {
if(indexs.includes(index)){
if (indexs.includes(index)) {
this.set(bit, true)
}else{
} else {
this.set(bit, false)
}
})
}
}
}
18 changes: 9 additions & 9 deletions src/classes/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/* eslint no-control-regex:off */
import {Logger} from '../'

describe("Logger", () => {
it("should log messages", () => {
describe('Logger', () => {
it('should log messages', () => {
console.log = jest.fn()

const logger = new Logger()

logger.log('foo')

expect((console.log as jest.Mock).mock.calls[0][0]).toContain("foo")
expect((console.log as jest.Mock).mock.calls[0][0]).toContain('foo')

logger.log('bar', true)

expect((console.log as jest.Mock).mock.calls[1][0]).toContain("bar")
expect((console.log as jest.Mock).mock.calls[1][0]).toContain('bar')

logger.error('widget')

expect((console.log as jest.Mock).mock.calls[2][0]).toContain("widget")
expect((console.log as jest.Mock).mock.calls[2][0]).toContain('widget')

const disabledLogger = new Logger('test', { output: false })
const disabledLogger = new Logger('test', {output: false})

disabledLogger.log('nope')

Expand All @@ -31,17 +31,17 @@ describe("Logger", () => {
expect((console.log as jest.Mock).mock.calls).toHaveLength(3)
})

it("should set a service name", () => {
it('should set a service name', () => {
console.log = jest.fn()

const testLogger = new Logger('Service')

testLogger.log('test')

expect((console.log as jest.Mock).mock.calls[0][0]).toContain("Service")
expect((console.log as jest.Mock).mock.calls[0][0]).toContain('Service')

testLogger.error('error', true)

expect((console.log as jest.Mock).mock.calls[1][0]).toContain("Service")
expect((console.log as jest.Mock).mock.calls[1][0]).toContain('Service')
})
})
Loading

0 comments on commit b01a22b

Please sign in to comment.