@pdfme/common 1.0.0-beta.7 → 1.0.0

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.
@@ -7,6 +7,8 @@ export declare const checkFont: (arg: {
7
7
  font: Font;
8
8
  template: Template;
9
9
  }) => void;
10
+ export declare const checkInputs: (data: unknown) => void;
11
+ export declare const checkUIOptions: (data: unknown) => void;
10
12
  export declare const checkTemplate: (data: unknown) => void;
11
13
  export declare const checkUIProps: (data: unknown) => void;
12
14
  export declare const checkPreviewProps: (data: unknown) => void;
@@ -1,6 +1,6 @@
1
1
  import { DEFAULT_FONT_SIZE, DEFAULT_ALIGNMENT, DEFAULT_LINE_HEIGHT, DEFAULT_CHARACTER_SPACING, DEFAULT_FONT_COLOR, BLANK_PDF } from './constants';
2
2
  import { schemaTypes, isImageSchema, isBarcodeSchema, isTextSchema } from './type';
3
3
  import type { Lang, Size, Alignment, SchemaType, BarCodeType, TextSchema, ImageSchema, BarcodeSchema, Schema, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, PreviewReactProps, DesignerProps, DesignerReactProps } from './type';
4
- import { getB64BasePdf, b64toUint8Array, getFallbackFontName, getDefaultFont, checkFont, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, validateBarcodeInput } from './helper';
5
- export { DEFAULT_FONT_SIZE, DEFAULT_ALIGNMENT, DEFAULT_LINE_HEIGHT, DEFAULT_CHARACTER_SPACING, DEFAULT_FONT_COLOR, BLANK_PDF, schemaTypes, isTextSchema, isImageSchema, isBarcodeSchema, getB64BasePdf, b64toUint8Array, getFallbackFontName, getDefaultFont, checkFont, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, validateBarcodeInput, };
4
+ import { getB64BasePdf, b64toUint8Array, getFallbackFontName, getDefaultFont, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, validateBarcodeInput } from './helper';
5
+ export { DEFAULT_FONT_SIZE, DEFAULT_ALIGNMENT, DEFAULT_LINE_HEIGHT, DEFAULT_CHARACTER_SPACING, DEFAULT_FONT_COLOR, BLANK_PDF, schemaTypes, isTextSchema, isImageSchema, isBarcodeSchema, getB64BasePdf, b64toUint8Array, getFallbackFontName, getDefaultFont, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, validateBarcodeInput, };
6
6
  export type { Lang, Size, Alignment, SchemaType, BarCodeType, TextSchema, ImageSchema, BarcodeSchema, Schema, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, PreviewReactProps, DesignerProps, DesignerReactProps, };
@@ -686,6 +686,7 @@ export declare const Template: z.ZodObject<{
686
686
  }>[];
687
687
  basePdf: string | ArrayBuffer | Uint8Array;
688
688
  }>;
689
+ export declare const Inputs: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodString>, "many">;
689
690
  export declare const CommonProps: z.ZodObject<{
690
691
  template: z.ZodObject<{
691
692
  schemas: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodObject<z.extendShape<{
@@ -2149,8 +2150,8 @@ export declare const PreviewProps: z.ZodObject<z.extendShape<z.extendShape<{
2149
2150
  }>[];
2150
2151
  basePdf: string | ArrayBuffer | Uint8Array;
2151
2152
  };
2152
- domContainer: HTMLElement;
2153
2153
  inputs: Record<string, string>[];
2154
+ domContainer: HTMLElement;
2154
2155
  }, {
2155
2156
  options?: {
2156
2157
  font?: Record<string, {
@@ -2200,8 +2201,8 @@ export declare const PreviewProps: z.ZodObject<z.extendShape<z.extendShape<{
2200
2201
  }>[];
2201
2202
  basePdf: string | ArrayBuffer | Uint8Array;
2202
2203
  };
2203
- domContainer: HTMLElement;
2204
2204
  inputs: Record<string, string>[];
2205
+ domContainer: HTMLElement;
2205
2206
  }>;
2206
2207
  export declare const PreviewReactProps: z.ZodObject<z.extendShape<{
2207
2208
  options: z.ZodOptional<z.ZodObject<z.extendShape<{
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@pdfme/common",
3
- "version": "1.0.0-beta.7",
3
+ "version": "1.0.0",
4
4
  "author": "hand-dot",
5
5
  "license": "MIT",
6
6
  "description": "TypeScript base PDF generator and React base UI. Open source, developed by the community, and completely free to use under the MIT license!",
7
7
  "homepage": "https://pdfme.com",
8
- "repository": "git@github.com:pdfme/pdfme.git",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git@github.com:pdfme/pdfme.git"
11
+ },
9
12
  "bugs": {
10
13
  "url": "https://github.com/pdfme/pdfme/issues"
11
14
  },
@@ -16,7 +19,7 @@
16
19
  "node": ">=14"
17
20
  },
18
21
  "scripts": {
19
- "develop": "webpack-dev-server --mode development",
22
+ "develop": "webpack --watch --mode development",
20
23
  "build": "NODE_ENV=production webpack --mode production",
21
24
  "clean": "rimraf dist",
22
25
  "lint": "tsc --noEmit",
@@ -51,4 +54,4 @@
51
54
  "publishConfig": {
52
55
  "access": "public"
53
56
  }
54
- }
57
+ }
package/src/helper.ts CHANGED
@@ -2,6 +2,8 @@ import { z } from 'zod';
2
2
  import Helvetica from './assets/Helvetica.ttf';
3
3
  import { Template, Schema, BasePdf, Font, CommonProps, isTextSchema, BarCodeType } from './type';
4
4
  import {
5
+ Inputs as InputsSchema,
6
+ UIOptions as UIOptionsSchema,
5
7
  Template as TemplateSchema,
6
8
  PreviewProps as PreviewPropsSchema,
7
9
  DesignerProps as DesignerPropsSchema,
@@ -40,13 +42,18 @@ export const getB64BasePdf = (basePdf: BasePdf) => {
40
42
  return basePdf as string;
41
43
  };
42
44
 
45
+ const getByteString = (base64: string) => {
46
+ if (typeof window !== 'undefined' && window.atob) {
47
+ return window.atob(base64);
48
+ } else {
49
+ return Buffer.from(base64, 'base64').toString('binary');
50
+ }
51
+ };
52
+
43
53
  export const b64toUint8Array = (base64: string) => {
44
54
  const data = base64.split(';base64,')[1] ? base64.split(';base64,')[1] : base64;
45
55
 
46
- const byteString =
47
- typeof window !== 'undefined'
48
- ? window.atob(data)
49
- : Buffer.from(data, 'base64').toString('binary');
56
+ const byteString = getByteString(data);
50
57
 
51
58
  const unit8arr = new Uint8Array(byteString.length);
52
59
  for (let i = 0; i < byteString.length; i += 1) {
@@ -135,12 +142,36 @@ ${message}`);
135
142
  }
136
143
  };
137
144
 
145
+ export const checkInputs = (data: unknown) => checkProps(data, InputsSchema);
146
+ export const checkUIOptions = (data: unknown) => checkProps(data, UIOptionsSchema);
138
147
  export const checkTemplate = (data: unknown) => checkProps(data, TemplateSchema);
139
148
  export const checkUIProps = (data: unknown) => checkProps(data, UIPropsSchema);
140
149
  export const checkPreviewProps = (data: unknown) => checkProps(data, PreviewPropsSchema);
141
150
  export const checkDesignerProps = (data: unknown) => checkProps(data, DesignerPropsSchema);
142
151
  export const checkGenerateProps = (data: unknown) => checkProps(data, GeneratePropsSchema);
143
152
 
153
+ // GTIN-13, GTIN-8, GTIN-12, GTIN-14
154
+ const validateCheckDigit = (input: string, checkDigitPos: number) => {
155
+ let passCheckDigit = true;
156
+
157
+ if (input.length === checkDigitPos) {
158
+ const ds = input.slice(0, -1).replace(/[^0-9]/g, '');
159
+ let sum = 0;
160
+ let odd = 1;
161
+ for (let i = ds.length - 1; i > -1; i -= 1) {
162
+ sum += Number(ds[i]) * (odd ? 3 : 1);
163
+ odd ^= 1;
164
+ if (sum > 0xffffffffffff) {
165
+ // ~2^48 at max
166
+ sum %= 10;
167
+ }
168
+ }
169
+ passCheckDigit = String(10 - (sum % 10)).slice(-1) === input.slice(-1);
170
+ }
171
+
172
+ return passCheckDigit;
173
+ };
174
+
144
175
  export const validateBarcodeInput = (type: BarCodeType, input: string) => {
145
176
  if (!input) return false;
146
177
  if (type === 'qrcode') {
@@ -157,13 +188,13 @@ export const validateBarcodeInput = (type: BarCodeType, input: string) => {
157
188
  // 有効文字は数値(0-9)のみ。チェックデジットを含まない12桁orチェックデジットを含む13桁。
158
189
  const regexp = /^\d{12}$|^\d{13}$/;
159
190
 
160
- return regexp.test(input);
191
+ return regexp.test(input) && validateCheckDigit(input, 13);
161
192
  }
162
193
  if (type === 'ean8') {
163
194
  // 有効文字は数値(0-9)のみ。チェックデジットを含まない7桁orチェックデジットを含む8桁。
164
195
  const regexp = /^\d{7}$|^\d{8}$/;
165
196
 
166
- return regexp.test(input);
197
+ return regexp.test(input) && validateCheckDigit(input, 8);
167
198
  }
168
199
  if (type === 'code39') {
169
200
  // 有効文字は数字(0-9)。アルファベット大文字(A-Z)、記号(-.$/+%)、半角スペース。
@@ -189,20 +220,20 @@ export const validateBarcodeInput = (type: BarCodeType, input: string) => {
189
220
  // 有効文字は数値(0-9)のみ。 チェックデジットを含まない13桁orチェックデジットを含む14桁。
190
221
  const regexp = /^\d{13}$|^\d{14}$/;
191
222
 
192
- return regexp.test(input);
223
+ return regexp.test(input) && validateCheckDigit(input, 14);
193
224
  }
194
225
  if (type === 'upca') {
195
226
  // 有効文字は数値(0-9)のみ。 チェックデジットを含まない11桁orチェックデジットを含む12桁。
196
227
  const regexp = /^\d{11}$|^\d{12}$/;
197
228
 
198
- return regexp.test(input);
229
+ return regexp.test(input) && validateCheckDigit(input, 12);
199
230
  }
200
231
  if (type === 'upce') {
201
232
  // 有効文字は数値(0-9)のみ。 1桁目に指定できる数字(ナンバーシステムキャラクタ)は0のみ。
202
233
  // チェックデジットを含まない7桁orチェックデジットを含む8桁。
203
234
  const regexp = /^0(\d{6}$|\d{7}$)/;
204
235
 
205
- return regexp.test(input);
236
+ return regexp.test(input) && validateCheckDigit(input, 8);
206
237
  }
207
238
 
208
239
  return false;
package/src/index.ts CHANGED
@@ -37,6 +37,8 @@ import {
37
37
  getFallbackFontName,
38
38
  getDefaultFont,
39
39
  checkFont,
40
+ checkInputs,
41
+ checkUIOptions,
40
42
  checkTemplate,
41
43
  checkUIProps,
42
44
  checkPreviewProps,
@@ -61,6 +63,8 @@ export {
61
63
  getFallbackFontName,
62
64
  getDefaultFont,
63
65
  checkFont,
66
+ checkInputs,
67
+ checkUIOptions,
64
68
  checkTemplate,
65
69
  checkUIProps,
66
70
  checkPreviewProps,
package/src/schema.ts CHANGED
@@ -37,10 +37,8 @@ export const TextSchema = CommonSchema.extend({
37
37
  });
38
38
 
39
39
  export const ImageSchema = CommonSchema.extend({ type: z.literal(SchemaType.Enum.image) });
40
- type ImageSchema = z.infer<typeof ImageSchema>;
41
40
 
42
41
  export const BarcodeSchema = CommonSchema.extend({ type: BarcodeSchemaType });
43
- type BarcodeSchema = z.infer<typeof BarcodeSchema>;
44
42
 
45
43
  export const Schema = z.union([TextSchema, ImageSchema, BarcodeSchema]);
46
44
 
@@ -68,7 +66,7 @@ export const Template = z.object({
68
66
  columns: z.array(z.string()).optional(),
69
67
  });
70
68
 
71
- const Inputs = z.array(z.record(z.string())).min(1);
69
+ export const Inputs = z.array(z.record(z.string())).min(1);
72
70
 
73
71
  const CommonOptions = z.object({ font: Font.optional() });
74
72
 
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "noImplicitAny": true,
4
- "target": "es5",
4
+ "target": "ES2015",
5
5
  "esModuleInterop": true,
6
6
  "isolatedModules": true,
7
7
  "declaration": true,
package/webpack.config.js CHANGED
@@ -35,7 +35,6 @@ const config = {
35
35
  filename: 'index.js',
36
36
  globalObject: 'this',
37
37
  library: {
38
- name: pkg.name,
39
38
  type: 'umd',
40
39
  },
41
40
  },