@magic/semver 0.0.16 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,11 +22,13 @@ semantic version parsing and stringification
22
22
  [snyk-url]: https://snyk.io/test/github/magic/semver
23
23
 
24
24
  #### installation:
25
+
25
26
  ```javascript
26
27
  npm install @magic/semver
27
28
  ```
28
29
 
29
30
  #### usage:
31
+
30
32
  ```javascript
31
33
  import semver from '@magic/semver'
32
34
 
@@ -40,61 +42,82 @@ const parsed = semver.parse(version)
40
42
  const serialized = semver.serialize(parsed)
41
43
 
42
44
  // version === serialized
43
-
44
45
  ```
45
46
 
46
47
  ##### changelog
47
48
 
48
49
  ##### 0.0.1
50
+
49
51
  first release
50
52
 
51
53
  ##### 0.0.2
54
+
52
55
  add bump and isSemver
53
56
 
54
57
  ##### 0.0.3
58
+
55
59
  handle -alpha and -beta separately to stop decrementing the version by mistake.
56
60
 
57
61
  ##### 0.0.4
62
+
58
63
  fix behaviour of appending -alpha.0 and -beta.0.
59
64
  these two edgecases also bump patch version now.
60
65
 
61
66
  ##### 0.0.5
67
+
62
68
  add isBigger and isSmaller version comparison
63
69
 
64
70
  ##### 0.0.6
71
+
65
72
  remove @magic/fs
66
73
 
67
- ##### 0.0.7
74
+ ##### 0.0.7
75
+
68
76
  bump required node version to 14.2.0
69
77
 
70
- ##### 0.0.8
78
+ ##### 0.0.8
79
+
71
80
  update dependencies
72
81
 
73
82
  ##### 0.0.9
74
- * bump required node version to 14.15.4
75
- * update dependencies
83
+
84
+ - bump required node version to 14.15.4
85
+ - update dependencies
76
86
 
77
87
  ##### 0.0.10
78
- * update README
79
- * bump dev dependencies
80
88
 
81
- ##### 0.0.11
89
+ - update README
90
+ - bump dev dependencies
91
+
92
+ ##### 0.0.11
93
+
82
94
  update dependencies
83
95
 
84
96
  ##### 0.0.12
97
+
85
98
  update dependencies
86
99
 
87
100
  ##### 0.0.13
101
+
88
102
  update dependencies
89
103
 
90
104
  ##### 0.0.14
105
+
91
106
  update dependencies
92
107
 
93
108
  ##### 0.0.15
109
+
94
110
  update dependencies
95
111
 
96
112
  ##### 0.0.16
113
+
97
114
  update dependencies
98
115
 
99
- ##### 0.0.17 - unreleased
116
+ ##### 0.0.17
117
+
118
+ - add typescript
119
+ - update dependencies
120
+
121
+ ##### 0.0.18 - unreleased
122
+
100
123
  ...
package/package.json CHANGED
@@ -1,22 +1,29 @@
1
1
  {
2
2
  "name": "@magic/semver",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "author": "Wizards & Witches",
5
5
  "description": "semantic version parsing and serialization.",
6
6
  "license": "AGPL-3.0",
7
7
  "homepage": "https://magic.github.io/semver",
8
- "main": "src/index.mjs",
8
+ "main": "src/index.js",
9
+ "types": "types/index.d.ts",
10
+ "module": "src/index.js",
9
11
  "type": "module",
10
12
  "scripts": {
11
- "start": "t -p",
13
+ "start": "npm test",
14
+ "build": "npm run build:docs",
15
+ "build:types": "tsc && npm run format",
16
+ "prepublishOnly": "npm run test && npm run build:types",
12
17
  "test": "t --exclude docs example config.js",
13
18
  "format": "f -w --exclude docs",
14
19
  "format:check": "f",
15
20
  "calls": "calls",
16
- "build": "NODE_ENV=production magic build",
21
+ "build:docs": "NODE_ENV=production magic build",
17
22
  "prod": "NODE_ENV=production magic build serve",
18
23
  "clean": "magic clean",
19
- "dev": "magic dev"
24
+ "serve": "magic serve",
25
+ "dev": "magic dev",
26
+ "lint": "tsc --noEmit"
20
27
  },
21
28
  "engine": {
22
29
  "node": ">=14.15.4"
@@ -30,21 +37,23 @@
30
37
  "url": "https://github.com/magic/semver/issues"
31
38
  },
32
39
  "dependencies": {
33
- "@magic/error": "0.0.16",
34
- "@magic/types": "0.1.22"
40
+ "@magic/error": "0.0.21",
41
+ "@magic/types": "0.1.31"
35
42
  },
36
43
  "devDependencies": {
37
44
  "@magic-modules/git-badges": "0.0.12",
38
- "@magic-modules/light-switch": "0.0.11",
39
- "@magic-modules/no-spy": "0.0.7",
40
- "@magic-modules/pre": "0.0.11",
41
- "@magic-themes/docs": "0.0.14",
42
- "@magic/core": "0.0.142",
43
- "@magic/format": "0.0.41",
44
- "@magic/test": "0.2.12"
45
+ "@magic-modules/light-switch": "0.0.12",
46
+ "@magic-modules/no-spy": "0.0.9",
47
+ "@magic-modules/pre": "0.0.12",
48
+ "@magic-themes/docs": "0.0.15",
49
+ "@magic/core": "0.0.156",
50
+ "@magic/format": "0.0.69",
51
+ "@magic/test": "0.2.28",
52
+ "typescript": "5.9.3"
45
53
  },
46
54
  "files": [
47
- "src"
55
+ "src",
56
+ "types"
48
57
  ],
49
58
  "contributors": [
50
59
  {
@@ -1,22 +1,39 @@
1
- import path from 'path'
2
-
3
1
  import error from '@magic/error'
4
2
  import is from '@magic/types'
5
3
 
6
- import { parse } from './parse.mjs'
7
- import { serialize } from './serialize.mjs'
8
- import { isSemver } from './isSemver.mjs'
4
+ import { parse } from './parse.js'
5
+ import { serialize } from './serialize.js'
6
+ import { isSemver } from './isSemver.js'
9
7
 
10
8
  const libName = '@magic/semver.bump'
11
9
 
12
- export const bump = (version, options = {}) => {
13
- if (is.empty(version)) {
10
+ /**
11
+ * @typedef {import('./types.js').Version} Version
12
+ */
13
+
14
+ /**
15
+ * @param {string} v
16
+ * @param {object} options
17
+ * @param {boolean} [options.major]
18
+ * @param {boolean} [options.minor]
19
+ * @param {boolean} [options.patch]
20
+ * @param {boolean} [options.beta]
21
+ * @param {boolean} [options.alpha]
22
+ * @returns {Version | string}
23
+ */
24
+ export const bump = (v, options = {}) => {
25
+ if (is.empty(v)) {
14
26
  throw error(`${libName} expects arguments to be non-empty`, 'E_VERSION_EMPTY')
15
27
  }
16
28
 
17
- const isString = is.string(version)
29
+ /** @type {Partial<Version> } */
30
+ let version
31
+
32
+ const isString = is.string(v)
18
33
  if (isString) {
19
- version = parse(version)
34
+ version = parse(v)
35
+ } else {
36
+ version = v
20
37
  }
21
38
 
22
39
  if (!isSemver(version)) {
@@ -30,16 +47,16 @@ export const bump = (version, options = {}) => {
30
47
  version.major += 1
31
48
  version.minor = 0
32
49
  version.patch = 0
33
- version.demo = {}
50
+ version.demo = undefined
34
51
  } else if (options.minor) {
35
52
  version.minor += 1
36
53
  version.patch = 0
37
- version.demo = {}
54
+ version.demo = undefined
38
55
  } else if (options.patch) {
39
56
  version.patch += 1
40
- version.demo = {}
57
+ version.demo = undefined
41
58
  } else if (options.beta) {
42
- if (is.empty(version.demo)) {
59
+ if (!version.demo) {
43
60
  version.patch += 1
44
61
  version.demo = {
45
62
  string: 'beta',
@@ -54,32 +71,35 @@ export const bump = (version, options = {}) => {
54
71
  version.demo.version += 1
55
72
  }
56
73
  } else if (options.alpha) {
57
- if (is.empty(version.demo)) {
58
- version.patch += 1
74
+ if (version.demo?.string === 'beta') {
75
+ throw error(
76
+ `${libName} got request to set ${JSON.stringify({ version })} to alpha. can not decrement versions.`,
77
+ 'E_NO_DECREMENT',
78
+ )
79
+ }
59
80
 
81
+ if (!version.demo) {
82
+ version.patch += 1
60
83
  version.demo = {
61
84
  string: 'alpha',
62
85
  version: 0,
63
86
  }
64
- } else {
65
- if (version.demo.string !== 'alpha') {
66
- throw error(
67
- `${libName} got request to set ${version.demo.string} to alpha. can not decrement versions.`,
68
- 'E_NO_DECREMENT',
69
- )
70
- }
71
-
87
+ } else if (version.demo.string === 'alpha') {
72
88
  version.demo.version += 1
73
89
  }
74
90
  } else {
75
91
  // no arguments given. bump alpha version if given, patch if not
76
- if (!is.empty(version.demo)) {
92
+ if (!is.undefined(version.demo)) {
77
93
  version.demo.version += 1
78
94
  } else {
79
95
  version.patch += 1
80
96
  }
81
97
  }
82
98
 
99
+ if (is.empty(version.demo)) {
100
+ delete version.demo
101
+ }
102
+
83
103
  if (isString) {
84
104
  return serialize(version)
85
105
  }
@@ -1,8 +1,8 @@
1
- import { bump as b } from './bump.mjs'
2
- import { isBigger as isB, isSmaller as isS } from './length.mjs'
3
- import { isSemver as is } from './isSemver.mjs'
4
- import { parse as p } from './parse.mjs'
5
- import { serialize as s } from './serialize.mjs'
1
+ import { bump as b } from './bump.js'
2
+ import { isBigger as isB, isSmaller as isS } from './length.js'
3
+ import { isSemver as is } from './isSemver.js'
4
+ import { parse as p } from './parse.js'
5
+ import { serialize as s } from './serialize.js'
6
6
 
7
7
  export const bump = b
8
8
  export const isBigger = isB
@@ -0,0 +1,28 @@
1
+ import is from '@magic/types'
2
+ import { parse } from './parse.js'
3
+ import { serialize } from './serialize.js'
4
+
5
+ /**
6
+ * @typedef {import('./types.js').Version} Version
7
+ */
8
+
9
+ /**
10
+ * @param {Version | string | unknown} v
11
+ * @returns {v is Version}
12
+ *
13
+ */
14
+ export const isSemver = v => {
15
+ try {
16
+ if (is.string(v)) {
17
+ const p = parse(v)
18
+
19
+ return is.obj(p) && is.num(p.major) && is.num(p.minor) && is.num(p.patch)
20
+ } else {
21
+ const s = serialize(v)
22
+
23
+ return is.str(s) && is.num(parseInt(s.split('.')[1]))
24
+ }
25
+ } catch {}
26
+
27
+ return false
28
+ }
@@ -1,6 +1,14 @@
1
1
  import is from '@magic/types'
2
2
 
3
- import { parse } from './parse.mjs'
3
+ import { parse } from './parse.js'
4
+ import { isSemver } from './isSemver.js'
5
+
6
+ /**
7
+ *
8
+ * @param {string} a
9
+ * @param {string} b
10
+ * @returns {boolean}
11
+ */
4
12
 
5
13
  export const isBigger = (a, b) => {
6
14
  if (a === b) {
@@ -10,6 +18,10 @@ export const isBigger = (a, b) => {
10
18
  const pA = parse(a)
11
19
  const pB = parse(b)
12
20
 
21
+ if (!isSemver(a) || !isSemver(b)) {
22
+ return false
23
+ }
24
+
13
25
  if (pA.major > pB.major) {
14
26
  return true
15
27
  }
@@ -35,18 +47,30 @@ export const isBigger = (a, b) => {
35
47
  return false
36
48
  }
37
49
 
38
- if (pA.demo.string === 'beta' && pB.demo.string === 'alpha') {
39
- return true
40
- }
41
-
42
- if (pA.demo.string === pB.demo.string) {
43
- if (pA.demo.version > pB.demo.version) {
50
+ if (pA.demo && pB.demo) {
51
+ if (pA.demo.string === 'beta' && pB.demo.string === 'alpha') {
44
52
  return true
45
53
  }
54
+
55
+ if (pA.demo.string === pB.demo.string) {
56
+ if (pA.demo.version > pB.demo.version) {
57
+ return true
58
+ }
59
+ }
60
+ } else if (!pA.demo && pB.demo) {
61
+ return true
62
+ } else if (pA.demo && !pB.demo) {
63
+ return false
46
64
  }
47
65
  }
48
66
 
49
67
  return false
50
68
  }
51
69
 
70
+ /**
71
+ *
72
+ * @param {string} a
73
+ * @param {string} b
74
+ * @returns
75
+ */
52
76
  export const isSmaller = (a, b) => !isBigger(a, b)
@@ -3,6 +3,15 @@ import error from '@magic/error'
3
3
 
4
4
  const libName = `@magic/semver.parse:`
5
5
 
6
+ /**
7
+ * @typedef {import('./types.js').Version} Version
8
+ * @typedef {import('./types.js').VersionResult} VersionResult
9
+ */
10
+
11
+ /**
12
+ * @param {string} v
13
+ * @returns {VersionResult}
14
+ */
6
15
  export const parse = v => {
7
16
  if (is.empty(v)) {
8
17
  throw error(`${libName} first argument must be non-empty`, 'E_ARG_EMPTY')
@@ -18,10 +27,12 @@ export const parse = v => {
18
27
 
19
28
  const [major, minor, patch, demo] = v.split('.')
20
29
 
30
+ /** @type {VersionResult} */
21
31
  const result = {
22
32
  major: parseInt(major),
23
33
  minor: parseInt(minor),
24
- demo: {},
34
+ patch: -1,
35
+ v,
25
36
  }
26
37
 
27
38
  if (is.string(patch) && patch.includes('-')) {
@@ -42,11 +53,9 @@ export const parse = v => {
42
53
  throw error(`${libName} minor was not an Int: ${result.minor}`, 'E_MINOR_TYPE')
43
54
  } else if (!is.number(result.patch)) {
44
55
  throw error(`${libName} patch was not an Int: ${result.patch}`, 'E_PATCH_TYPE')
45
- } else if (!is.empty(result.demo) && !is.number(result.demo.version)) {
46
- throw error(`${libName} demo was not an Int: ${result.demo.version}`, 'E_DEMO_TYPE')
56
+ } else if (!is.empty(result.demo) && !is.number(result.demo?.version)) {
57
+ throw error(`${libName} demo was not an Int: ${result.demo?.version}`, 'E_DEMO_TYPE')
47
58
  }
48
59
 
49
- result.v = v
50
-
51
60
  return result
52
61
  }
@@ -3,29 +3,37 @@ import error from '@magic/error'
3
3
 
4
4
  const libName = `@magic/semver.serialize:`
5
5
 
6
+ /**
7
+ * @typedef {import('./types.js').Version} Version
8
+ */
9
+
10
+ /**
11
+ * @param {Version | unknown} v
12
+ * @returns {string}
13
+ */
6
14
  export const serialize = v => {
7
15
  if (is.empty(v)) {
8
16
  throw error(`${libName} v must be non-empty`, 'E_ARG_EMPTY')
9
17
  }
10
18
 
11
19
  // the || v.major is a cheap test to make sure we get a valid object
12
- if (!is.object(v) || !v.hasOwnProperty('major')) {
20
+ if (!is.objectNative(v) || !v.hasOwnProperty('major')) {
13
21
  throw error(`${libName} v must be an object`, 'E_ARG_TYPE')
14
22
  }
15
23
 
16
- const major = parseInt(v.major)
24
+ const major = v.major
17
25
 
18
26
  if (!is.number(major)) {
19
27
  throw error(`${libName} major was not an Int: ${major}`, 'E_MAJOR_TYPE')
20
28
  }
21
29
 
22
- const minor = parseInt(v.minor)
30
+ const minor = v.minor
23
31
 
24
32
  if (!is.number(minor)) {
25
33
  throw error(`${libName} minor was not an Int: ${minor}`, 'E_MINOR_TYPE')
26
34
  }
27
35
 
28
- const patch = parseInt(v.patch)
36
+ const patch = v.patch
29
37
 
30
38
  if (!is.number(patch)) {
31
39
  throw error(`${libName} patch was not an Int: ${patch}`, 'E_PATCH_TYPE')
@@ -33,9 +41,12 @@ export const serialize = v => {
33
41
 
34
42
  let demoString = ''
35
43
 
36
- if (!is.empty(v.demo)) {
44
+ if (!is.empty(v.demo) && is.objectNative(v.demo)) {
37
45
  let { version, string } = v.demo
38
- version = parseInt(version)
46
+
47
+ if (is.str(version)) {
48
+ version = parseInt(version)
49
+ }
39
50
 
40
51
  if (!is.number(version)) {
41
52
  throw error(`${libName} demo was not an Int: ${version}`, 'E_DEMO_TYPE')
package/src/types.ts ADDED
@@ -0,0 +1,11 @@
1
+ export type Version = {
2
+ major: number
3
+ minor: number
4
+ patch: number
5
+ demo?: {
6
+ string: string
7
+ version: number
8
+ }
9
+ }
10
+
11
+ export type VersionResult = Version & { v: string }
@@ -0,0 +1 @@
1
+ export function bump(version: any, options?: {}): any
@@ -0,0 +1,11 @@
1
+ export function bump(
2
+ v: string,
3
+ options?: {
4
+ major?: boolean | undefined
5
+ minor?: boolean | undefined
6
+ patch?: boolean | undefined
7
+ beta?: boolean | undefined
8
+ alpha?: boolean | undefined
9
+ },
10
+ ): Version | string
11
+ export type Version = import('./types.js').Version
@@ -0,0 +1,21 @@
1
+ export const bump: (version: any, options?: {}) => any
2
+ export const isBigger: (a: any, b: any) => boolean
3
+ export const isSemver: (v: any) => boolean
4
+ export const isSmaller: (a: any, b: any) => boolean
5
+ export const parse: (v: any) => {
6
+ major: number
7
+ minor: number
8
+ demo: {}
9
+ }
10
+ export const stringify: (v: any) => string
11
+ export const serialize: (v: any) => string
12
+ declare namespace _default {
13
+ export { bump }
14
+ export { isBigger }
15
+ export { isSemver }
16
+ export { isSmaller }
17
+ export { parse }
18
+ export { serialize }
19
+ export { stringify }
20
+ }
21
+ export default _default
@@ -0,0 +1,26 @@
1
+ export const bump: (
2
+ v: string,
3
+ options?: {
4
+ major?: boolean | undefined
5
+ minor?: boolean | undefined
6
+ patch?: boolean | undefined
7
+ beta?: boolean | undefined
8
+ alpha?: boolean | undefined
9
+ },
10
+ ) => Version | string
11
+ export const isBigger: (a: string, b: string) => boolean
12
+ export const isSemver: (v: Version | string | unknown) => v is Version
13
+ export const isSmaller: (a: string, b: string) => boolean
14
+ export const parse: (v: string) => VersionResult
15
+ export const stringify: (v: Version | unknown) => string
16
+ export const serialize: (v: Version | unknown) => string
17
+ declare namespace _default {
18
+ export { bump }
19
+ export { isBigger }
20
+ export { isSemver }
21
+ export { isSmaller }
22
+ export { parse }
23
+ export { serialize }
24
+ export { stringify }
25
+ }
26
+ export default _default
@@ -0,0 +1 @@
1
+ export function isSemver(v: any): boolean
@@ -0,0 +1,2 @@
1
+ export function isSemver(v: Version | string | unknown): v is Version
2
+ export type Version = import('./types.js').Version
@@ -0,0 +1,2 @@
1
+ export function isBigger(a: any, b: any): boolean
2
+ export function isSmaller(a: any, b: any): boolean
@@ -0,0 +1,2 @@
1
+ export function isBigger(a: string, b: string): boolean
2
+ export function isSmaller(a: string, b: string): boolean
@@ -0,0 +1,5 @@
1
+ export function parse(v: any): {
2
+ major: number
3
+ minor: number
4
+ demo: {}
5
+ }
@@ -0,0 +1,3 @@
1
+ export function parse(v: string): VersionResult
2
+ export type Version = import('./types.js').Version
3
+ export type VersionResult = import('./types.js').VersionResult
@@ -0,0 +1 @@
1
+ export function serialize(v: any): string
@@ -0,0 +1,2 @@
1
+ export function serialize(v: Version | unknown): string
2
+ export type Version = import('./types.js').Version
@@ -0,0 +1,12 @@
1
+ export type Version = {
2
+ major: number
3
+ minor: number
4
+ patch: number
5
+ demo?: {
6
+ string: string
7
+ version: number
8
+ }
9
+ }
10
+ export type VersionResult = Version & {
11
+ v: string
12
+ }
package/src/isSemver.mjs DELETED
@@ -1,19 +0,0 @@
1
- import is from '@magic/types'
2
- import { parse } from './parse.mjs'
3
- import { serialize } from './serialize.mjs'
4
-
5
- export const isSemver = v => {
6
- try {
7
- if (is.string(v)) {
8
- const p = parse(v)
9
-
10
- return is.obj(p) && is.num(p.major)
11
- } else {
12
- const s = serialize(v)
13
-
14
- return is.str(s) && is.num(parseInt(s.split('.')[1]))
15
- }
16
- } catch (_) {
17
- return false
18
- }
19
- }