Skip to content

Commit

Permalink
1.2.1
Browse files Browse the repository at this point in the history
- Add constant `LATEST`
- Add type definitions
- Fix bugs
  • Loading branch information
Nixinova committed Apr 5, 2021
1 parent 289f5a3 commit a380eca
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 75 deletions.
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# General
* text=auto

# Project
package-lock.json -diff

# Linguist
*.ts linguist-language=TypeScript
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
*.js
*.d.ts
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 1.2.1
*2021-04-06*
- Added constant `LATEST` which returns the latest `pack_format` for both resource and data pack formats.
- Added type definitions to package.
- Fixed the automatically-generated current snapshot not working for single-digit weeks.
- Fixed `getVersions()` sometimes returning invalid maximum versions.
- Fixed `getVersions()` crashing when given a future pack version.

## 1.2.0
*2021-03-06*
- Added function `getVersions(pack_format: number, type?: string): object` to retrieve a list of versions that have the specified pack_format.
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pack-format",
"version": "1.2.0",
"version": "1.2.1",
"description": "Returns the pack_format of any Minecraft version, including snapshots",
"scripts": {
"prepublish": "tsc",
Expand All @@ -12,14 +12,13 @@
"datapack",
"pack_format"
],
"main": "index.js",
"main": "src/index.js",
"bin": {
"pack-format": "cli.js"
"pack-format": "src/cli.js"
},
"files": [
"index.js",
"cli.js",
"test.js"
"src/*.js",
"src/*.d.ts"
],
"repository": {
"type": "git",
Expand All @@ -32,6 +31,6 @@
"license": "ISC",
"devDependencies": {
"@types/node": "^14.14.31",
"typescript": "^4.2.2"
"typescript": "~4.2.3"
}
}
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ To install pack-format, open the command line and type `npm install pack-format`
### Node

Retrieve the `pack_format` of a given Minecraft version, optionally specifying whether the resource (default) or data pack version should be returned.

```js
const packFormat = require('pack-format')
packFormat('1.14.4') // 4
packFormat('1.16.2-pre1', 'resource') // 5
packFormat('20w45a', 'data') // 6
packFormat.LATEST.data // 7
```

Retrieve a list of versions corresponding to a specific `pack_format`, again optionally specifying resource/data pack version.

```js
const {getVersions} = require('pack-format')
getVersions(3) // { releases: { min: '1.11.x', max: '1.12.x' }, snapshots: { min: '16w32a', max: '17w47a' } }
Expand Down
45 changes: 32 additions & 13 deletions cli.ts → src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

const VERSION: string = '1.2.0'
const VERSION: string = '1.2.1'

import { getPackFormat, getVersions } from './index'
import { getPackFormat, getVersions, LATEST } from './index'

const arg = (n: number): string => process.argv[n + 1]
const indent = (n: number): string => ' '.repeat(n * 4)
Expand All @@ -13,18 +13,32 @@ const log = function (arg: string, desc: string[], example: string): void {
console.log(`${indent(3)}Example: ${example}`)
}

if (arg(1) && !arg(1).includes('h'))
if (/^-*v/.test(arg(1)))
if (arg(1) && !arg(1).includes('h')) {
const cmd = arg(1)
if (/^-*v/.test(cmd)) {
console.log(`The current version of pack-format is ${VERSION}`)
else if (/^-*d/.test(arg(1)))
console.log(`Data pack format of ${arg(2)} is ${getPackFormat(arg(2), 'data')}`)
else if (/^-*r/.test(arg(1)))
console.log(`Resource pack format of ${arg(2)} is ${getPackFormat(arg(2), 'resource')}`)
else if (/^-*l/.test(arg(1)))
}
else if (/^-*d/.test(cmd)) {
const ver = arg(2)
console.log(`Data pack format of ${ver} is ${getPackFormat(ver, 'data')}`)
}
else if (/^-*r/.test(cmd)) {
const ver = arg(2)
console.log(`Resource pack format of ${ver} is ${getPackFormat(ver, 'resource')}`)
}
else if (/^-*l/.test(cmd)) {
if (arg(3)) console.log(getVersions(+arg(3), /^-*d/.test(arg(2)) ? 'data' : 'resource'))
else console.log(getVersions(+arg(2)))
else
console.log(`Pack format of ${arg(1)} is ${getPackFormat(arg(1))}`)
}
else if (/^-*L/.test(cmd)) {
const type = /^-*d/.test(arg(2)) ? 'data' : 'resource'
if (arg(2)) console.log(`The latest ${type} pack version is ${LATEST[type]}.`)
else console.log(`The latest pack version is ${LATEST.resource}.`)
}
else {
console.log(`Pack format of ${cmd} is ${getPackFormat(cmd)}`)
}
}
else {
console.log(`\n${indent(1)}pack-format arguments:`)
log(
Expand All @@ -38,13 +52,18 @@ else {
'pack-format --data 20w45a',
)
log(
'(--resource|-r) <version>',
'(--resource|-r) <version>',
['Retrieve the resource pack format in particular when applicable.'],
'pack-format -r 20w45a',
)
log(
'(--list|-l) [(--data|-d)|(--resource|-r)] <pack_format>',
'(--list|-l) [(--data|-d)|(--resource|-r)] <pack_format>',
['Retrieve a list of versions attached to a specific pack format.'],
'pack-format --list -d 6',
)
log(
'(--latest|-L) [(--data|-d)|(--resource|-r)]',
['Retrieve the latest pack format.'],
'pack-format --latest --resource',
)
}
44 changes: 23 additions & 21 deletions index.ts → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { packType, formatResult, versionsResult } from './types'
import { VersionName, SnapshotName, PackType, FormatResult, VersionsResult } from './types'

class Snapshot {
version: string
Expand All @@ -8,7 +8,7 @@ class Snapshot {
get id(): number { return (this.year - 10) * 52 + this.week }
}

const RELEASES: Record<number, string[]> = {
const RELEASES: Record<number, VersionName[]> = {
1: ['1.6.x', '1.7.x', '1.8.x'],
2: ['1.9.x', '1.10.x'],
3: ['1.11.x', '1.12.x'],
Expand All @@ -18,30 +18,31 @@ const RELEASES: Record<number, string[]> = {
7: ['1.17.x'],
}

const d: Date = new Date()
const fauxCurrentSnapshot: string = (d.getFullYear() - 2000) + 'w' + ((d.getMonth() + 1) * 5) + 'a'
const START_SNAPSHOTS: Record<string, Record<packType, formatResult>> = {
const d = new Date()
const fauxCurrentSnapshot = (d.getFullYear() - 2000) + 'w' + ((d.getMonth() + 1) * 5).toString().padStart(2, '0') + 'a' as SnapshotName
const START_SNAPSHOTS: Record<SnapshotName | string, Record<PackType, FormatResult>> = {
'13w24a': { resource: 1, data: undefined },
'15w31a': { resource: 2, data: undefined },
'16w32a': { resource: 3, data: undefined },
'17w48a': { resource: 4, data: 4 },
'20w06a': { resource: 5, data: 5 },
'20w45a': { resource: 7, data: 6 },
'20w46a': { resource: 7, data: 7 },
[fauxCurrentSnapshot]: { resource: undefined, data: undefined }
[fauxCurrentSnapshot]: { resource: undefined, data: undefined },
}

function getPackFormat(version: string, type: packType = 'resource'): formatResult {
const LATEST = { resource: 7, data: 7 }

function getPackFormat(version: string, type: PackType = 'resource'): FormatResult {
if (!version) return undefined
version = version.toString().toLowerCase().trim()

// Snapshot //

if (/^\d\dw\d\d\w$/.test(version)) {
if (/^\d\d[w]\d\d[a-z]$/.test(version)) {
const snapshot = new Snapshot(version)

let ver: formatResult
for (let testSnap in START_SNAPSHOTS) {
let ver: FormatResult
for (let testSnap of Object.keys(START_SNAPSHOTS)) {
if (snapshot.id >= (new Snapshot(testSnap)).id) {
ver = START_SNAPSHOTS[testSnap][type]
}
Expand All @@ -67,34 +68,34 @@ function getPackFormat(version: string, type: packType = 'resource'): formatResu
if (matchExact || matchMinor) return +i
}
}

return undefined
}

function getVersions(format: number, type: packType = 'resource'): versionsResult {
let output = {
function getVersions(format: number, type: PackType = 'resource'): VersionsResult {
let output: VersionsResult = {
'releases': { 'min': '', 'max': '' },
'snapshots': { 'min': '', 'max': '' },
}
if (!format || (type === 'data' && format < 4)) return output
if (!format || format > LATEST[type] || (type === 'data' && format < 4)) return output

output.releases.min = RELEASES[format][0]
output.releases.max = RELEASES[format].slice(-1)[0]

const startSnaps = Object.keys(START_SNAPSHOTS)
for (let snap in START_SNAPSHOTS) {
const startSnaps: string[] = Object.keys(START_SNAPSHOTS)
for (const snap in START_SNAPSHOTS) {
if (START_SNAPSHOTS[snap][type] === format) {
let maxSnap: string = snap
let maxSnap = snap as SnapshotName
let i = 1
do {
let nextSnap = startSnaps[startSnaps.indexOf(snap) + i++]
const nextSnap = startSnaps[startSnaps.indexOf(snap) + i++] as SnapshotName
if (nextSnap) maxSnap = nextSnap
else break
}
while (getPackFormat(maxSnap, type) === getPackFormat(snap, type))

const prevSnap: string = maxSnap?.replace(/(\d)(?=\w$)/, (_, n) => String(+n - 1))
output.snapshots.min = snap
output.snapshots.max = prevSnap
output.snapshots.min = snap as SnapshotName
output.snapshots.max = maxSnap.replace(/(\d\d)[a-z]$/, (_, n) => (+n - 1).toString() + 'a') as SnapshotName
break
}
}
Expand All @@ -104,5 +105,6 @@ function getVersions(format: number, type: packType = 'resource'): versionsResul

getPackFormat.getPackFormat = getPackFormat
getPackFormat.getVersions = getVersions
getPackFormat.LATEST = LATEST

export = getPackFormat
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type VersionName = `1.${number}.${number | 'x'}`
export type SnapshotName = `${number}w${number}${string}`

export type PackType = 'resource' | 'data'
export type PackMap = Record<PackType, FormatResult>

export type FormatResult = number | undefined

export interface VersionsResult {
releases: { min: VersionName | '', max: VersionName | '' },
snapshots: { min: SnapshotName | '', max: SnapshotName | '' },
}
18 changes: 9 additions & 9 deletions test.ts → test/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getPackFormat as packFormat, getVersions } from './index'
import { packType, formatResult, versionsResult } from './types'
import { getPackFormat as packFormat, getVersions } from '../src/index'
import { PackType, FormatResult, VersionsResult } from '../src/types'

let total = 0, passed = 0, failed = 0
let [total, passed, failed]: number[] = [0, 0, 0]

function testPackFormat([input, type]: [string, packType?], expected: formatResult): void {
let ver = packFormat(input, type)
let pass = ver == expected
function testPackFormat([input, type]: [string, PackType?], expected: FormatResult): void {
let ver: FormatResult = packFormat(input, type)
let pass: boolean = ver === expected
if (pass) passed++
else failed++
total++
Expand All @@ -17,13 +17,13 @@ function testPackFormat([input, type]: [string, packType?], expected: formatResu
)
}

function testVersions([input, type]: [number, packType?], expected: versionsResult) {
let obj = getVersions(input, type)
function testVersions([input, type]: [number, PackType?], expected: VersionsResult) {
let obj: VersionsResult = getVersions(input, type)
let pass = obj.releases.max === expected.releases.max && obj.snapshots.min === expected.snapshots.min
if (pass) passed++
else failed++
total++
const cleanObj = (obj: versionsResult) => JSON.stringify(obj).replace(/"(?!")/g, '')
const cleanObj = (obj: VersionsResult) => JSON.stringify(obj).replace(/"(?!")/g, '')
console.log(
(pass ? ' ' : '! ')
+ `Versions with ${type ? type + ' ' : ''}pack format ${input}: ${cleanObj(obj)}`
Expand Down
Loading

0 comments on commit a380eca

Please sign in to comment.