@guanghechen/filepart 1.0.4 → 2.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present guanghechen (https://github.com/guanghechen)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,144 @@
1
+ <header>
2
+ <h1 align="center">
3
+ <a href="https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.0/packages/filepart#readme">@guanghechen/filepart</a>
4
+ </h1>
5
+ <div align="center">
6
+ <a href="https://www.npmjs.com/package/@guanghechen/filepart">
7
+ <img
8
+ alt="Npm Version"
9
+ src="https://img.shields.io/npm/v/@guanghechen/filepart.svg"
10
+ />
11
+ </a>
12
+ <a href="https://www.npmjs.com/package/@guanghechen/filepart">
13
+ <img
14
+ alt="Npm Download"
15
+ src="https://img.shields.io/npm/dm/@guanghechen/filepart.svg"
16
+ />
17
+ </a>
18
+ <a href="https://www.npmjs.com/package/@guanghechen/filepart">
19
+ <img
20
+ alt="Npm License"
21
+ src="https://img.shields.io/npm/l/@guanghechen/filepart.svg"
22
+ />
23
+ </a>
24
+ <a href="#install">
25
+ <img
26
+ alt="Module Formats: cjs, esm"
27
+ src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
28
+ />
29
+ </a>
30
+ <a href="https://github.com/nodejs/node">
31
+ <img
32
+ alt="Node.js Version"
33
+ src="https://img.shields.io/node/v/@guanghechen/filepart"
34
+ />
35
+ </a>
36
+ <a href="https://github.com/facebook/jest">
37
+ <img
38
+ alt="Tested with Jest"
39
+ src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
40
+ />
41
+ </a>
42
+ <a href="https://github.com/prettier/prettier">
43
+ <img
44
+ alt="Code Style: prettier"
45
+ src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
46
+ />
47
+ </a>
48
+ </div>
49
+ </header>
50
+ <br/>
51
+
52
+ File helper utilities for calculating file part ranges and generating part names. Useful for
53
+ splitting large files into smaller chunks.
54
+
55
+ ## Install
56
+
57
+ - npm
58
+
59
+ ```bash
60
+ npm install --save @guanghechen/filepart
61
+ ```
62
+
63
+ - yarn
64
+
65
+ ```bash
66
+ yarn add @guanghechen/filepart
67
+ ```
68
+
69
+ ## Usage
70
+
71
+ ### Calculate File Parts by Size
72
+
73
+ Split a file into parts of a specific size:
74
+
75
+ ```typescript
76
+ import { calcFilePartItemsBySize } from '@guanghechen/filepart'
77
+
78
+ const fileSize = 1000 // bytes
79
+ const partSize = 300 // bytes per part
80
+
81
+ const parts = [...calcFilePartItemsBySize(fileSize, partSize)]
82
+ // Result:
83
+ // [
84
+ // { sid: 1, start: 0, end: 300 },
85
+ // { sid: 2, start: 300, end: 600 },
86
+ // { sid: 3, start: 600, end: 900 },
87
+ // { sid: 4, start: 900, end: 1000 }
88
+ // ]
89
+ ```
90
+
91
+ ### Calculate File Parts by Count
92
+
93
+ Split a file into a specific number of parts:
94
+
95
+ ```typescript
96
+ import { calcFilePartItemsByCount } from '@guanghechen/filepart'
97
+
98
+ const fileSize = 1000 // bytes
99
+ const partCount = 3 // number of parts
100
+
101
+ const parts = [...calcFilePartItemsByCount(fileSize, partCount)]
102
+ // Result:
103
+ // [
104
+ // { sid: 1, start: 0, end: 334 },
105
+ // { sid: 2, start: 334, end: 668 },
106
+ // { sid: 3, start: 668, end: 1000 }
107
+ // ]
108
+ ```
109
+
110
+ ### Generate Part Names
111
+
112
+ Generate file part names with padded sequence numbers:
113
+
114
+ ```typescript
115
+ import { calcFilePartNames, calcFilePartNamesByCount } from '@guanghechen/filepart'
116
+
117
+ // From existing parts
118
+ const parts = [{ sid: 1 }, { sid: 2 }, { sid: 3 }]
119
+ const names = [...calcFilePartNames(parts, '.part')]
120
+ // Result: ['.part1', '.part2', '.part3']
121
+
122
+ // For 14 parts (with zero-padding)
123
+ const names14 = [...calcFilePartNamesByCount(14, '.part')]
124
+ // Result: ['.part01', '.part02', ... '.part14']
125
+
126
+ // Single part returns empty string (no suffix needed)
127
+ const singlePart = [...calcFilePartNamesByCount(1, '.part')]
128
+ // Result: ['']
129
+ ```
130
+
131
+ ### Constants
132
+
133
+ ```typescript
134
+ import { DEFAULT_FILEPART_CODE_PREFIX } from '@guanghechen/filepart'
135
+
136
+ console.log(DEFAULT_FILEPART_CODE_PREFIX) // '.ghc-part'
137
+ ```
138
+
139
+ ## Reference
140
+
141
+ - [homepage][homepage]
142
+
143
+ [homepage]:
144
+ https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.0/packages/filepart#readme
package/lib/cjs/index.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var invariant = require('@guanghechen/invariant');
4
- var filepart_types = require('@guanghechen/filepart.types');
4
+
5
+ const DEFAULT_FILEPART_CODE_PREFIX = '.ghc-part';
5
6
 
6
7
  function* calcFilePartItemsBySize(fileSize, partSize) {
7
8
  if (fileSize <= 0) {
@@ -74,13 +75,8 @@ function* calcFilePartNamesByCount(partTotal, partCodePrefix) {
74
75
  }
75
76
  }
76
77
 
78
+ exports.DEFAULT_FILEPART_CODE_PREFIX = DEFAULT_FILEPART_CODE_PREFIX;
77
79
  exports.calcFilePartItemsByCount = calcFilePartItemsByCount;
78
80
  exports.calcFilePartItemsBySize = calcFilePartItemsBySize;
79
81
  exports.calcFilePartNames = calcFilePartNames;
80
82
  exports.calcFilePartNamesByCount = calcFilePartNamesByCount;
81
- Object.keys(filepart_types).forEach(function (k) {
82
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
83
- enumerable: true,
84
- get: function () { return filepart_types[k]; }
85
- });
86
- });
package/lib/esm/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { invariant } from '@guanghechen/invariant';
2
- export * from '@guanghechen/filepart.types';
2
+
3
+ const DEFAULT_FILEPART_CODE_PREFIX = '.ghc-part';
3
4
 
4
5
  function* calcFilePartItemsBySize(fileSize, partSize) {
5
6
  if (fileSize <= 0) {
@@ -72,4 +73,4 @@ function* calcFilePartNamesByCount(partTotal, partCodePrefix) {
72
73
  }
73
74
  }
74
75
 
75
- export { calcFilePartItemsByCount, calcFilePartItemsBySize, calcFilePartNames, calcFilePartNamesByCount };
76
+ export { DEFAULT_FILEPART_CODE_PREFIX, calcFilePartItemsByCount, calcFilePartItemsBySize, calcFilePartNames, calcFilePartNamesByCount };
@@ -1,5 +1,22 @@
1
- import { IFilePartItem } from '@guanghechen/filepart.types';
2
- export * from '@guanghechen/filepart.types';
1
+ declare const DEFAULT_FILEPART_CODE_PREFIX = ".ghc-part";
2
+
3
+ /**
4
+ * An part item of file.
5
+ */
6
+ interface IFilePartItem {
7
+ /**
8
+ * Sequence no of a part.
9
+ */
10
+ sid: number;
11
+ /**
12
+ * Start position of a part in sourcefile.
13
+ */
14
+ start: number;
15
+ /**
16
+ * End position of a part in sourcefile.
17
+ */
18
+ end: number;
19
+ }
3
20
 
4
21
  /**
5
22
  * Generate file part items by part size.
@@ -27,4 +44,5 @@ declare function calcFilePartItemsByCount(fileSize: number, partTotal: number):
27
44
  declare function calcFilePartNames(parts: ReadonlyArray<Pick<IFilePartItem, 'sid'>>, partCodePrefix: string): IterableIterator<string>;
28
45
  declare function calcFilePartNamesByCount(partTotal: number, partCodePrefix: string): IterableIterator<string>;
29
46
 
30
- export { calcFilePartItemsByCount, calcFilePartItemsBySize, calcFilePartNames, calcFilePartNamesByCount };
47
+ export { DEFAULT_FILEPART_CODE_PREFIX, calcFilePartItemsByCount, calcFilePartItemsBySize, calcFilePartNames, calcFilePartNamesByCount };
48
+ export type { IFilePartItem };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/filepart",
3
- "version": "1.0.4",
3
+ "version": "2.0.1",
4
4
  "description": "File helper",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -8,10 +8,10 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/guanghechen/sora/tree/@guanghechen/filepart@1.0.3",
11
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.0",
12
12
  "directory": "packages/filepart"
13
13
  },
14
- "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/filepart@1.0.3/packages/filepart#readme",
14
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/filepart@2.0.0/packages/filepart#readme",
15
15
  "keywords": [
16
16
  "file helper",
17
17
  "split file",
@@ -20,10 +20,10 @@
20
20
  "type": "module",
21
21
  "exports": {
22
22
  ".": {
23
+ "types": "./lib/types/index.d.ts",
23
24
  "source": "./src/index.ts",
24
25
  "import": "./lib/esm/index.mjs",
25
- "require": "./lib/cjs/index.cjs",
26
- "types": "./lib/types/index.d.ts"
26
+ "require": "./lib/cjs/index.cjs"
27
27
  }
28
28
  },
29
29
  "source": "./src/index.ts",
@@ -40,8 +40,13 @@
40
40
  "README.md"
41
41
  ],
42
42
  "dependencies": {
43
- "@guanghechen/filepart.types": "^1.0.3",
44
- "@guanghechen/invariant": "^6.0.5"
43
+ "@guanghechen/invariant": "^7.0.1"
45
44
  },
46
- "gitHead": "163ec00ff9d6f2d26673bccd3b6f6f31db195d7a"
47
- }
45
+ "scripts": {
46
+ "build": "rollup -c ../../rollup.config.mjs",
47
+ "clean": "rimraf lib",
48
+ "test": "vitest run --config ../../vitest.config.ts",
49
+ "test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
50
+ "test:update": "vitest run --config ../../vitest.config.ts -u"
51
+ }
52
+ }