@libs-ui/pipes-check-file-extension 0.2.355-9 → 0.2.356-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.
Files changed (2) hide show
  1. package/README.md +163 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -0,0 +1,163 @@
1
+ # @libs-ui/pipes-check-file-extension
2
+
3
+ > Version: `0.2.355-10`
4
+ >
5
+ > Pipe kiểm tra định dạng file dựa trên extension (đuôi file) để phân loại thành các nhóm định dạng phổ biến.
6
+
7
+ ## Giới thiệu
8
+
9
+ `LibsUiPipesCheckFileExtensionPipe` giúp xác định nhanh chóng một file có thuộc nhóm định dạng cụ thể (như image, video, pdf...) hay không. Kết quả trả về là `boolean` (`true` nếu khớp, `false` nếu không).
10
+
11
+ ### Tính năng
12
+
13
+ - ✅ Kiểm tra định dạng file dựa trên extension
14
+ - ✅ Hỗ trợ các nhóm: image, video, word, xlsx, pdf, pptx, other
15
+ - ✅ Hỗ trợ MIME type cho các loại văn bản
16
+ - ✅ Pure logic, hiệu năng cao
17
+ - ✅ Standalone pipe (Angular 16+)
18
+
19
+ ## Khi nào sử dụng
20
+
21
+ - Khi cần kiểm tra loại file dựa trên phần mở rộng (extension) để hiển thị icon hoặc UI tương ứng.
22
+ - Hỗ trợ các nhóm định dạng: image, video, word, xlsx, pdf, pptx, other.
23
+ - Sử dụng trong danh sách file, upload preview, hoặc file attachment.
24
+
25
+ ## Cài đặt
26
+
27
+ ```bash
28
+ npm install @libs-ui/pipes-check-file-extension
29
+ ```
30
+
31
+ ## Import
32
+
33
+ ```typescript
34
+ import { LibsUiPipesCheckFileExtensionPipe } from '@libs-ui/pipes-check-file-extension';
35
+
36
+ @Component({
37
+ standalone: true,
38
+ imports: [LibsUiPipesCheckFileExtensionPipe],
39
+ // ...
40
+ })
41
+ ```
42
+
43
+ ## Cách sử dụng
44
+
45
+ ### 1. Kiểm tra Image
46
+
47
+ ```html
48
+ {{ fileImage | LibsUiPipesCheckFileExtensionPipe : 'image' }}
49
+ ```
50
+
51
+ ```typescript
52
+ readonly fileImage: IFile = { name: 'holiday-photo.jpg', size: '2 MB' };
53
+ ```
54
+
55
+ ### 2. Kiểm tra Video
56
+
57
+ ```html
58
+ {{ fileVideo | LibsUiPipesCheckFileExtensionPipe : 'video' }}
59
+ ```
60
+
61
+ ```typescript
62
+ readonly fileVideo: IFile = { name: 'presentation.mp4', size: '10 MB' };
63
+ ```
64
+
65
+ ### 3. Kiểm tra sai loại (False)
66
+
67
+ ```html
68
+ {{ fileDoc | LibsUiPipesCheckFileExtensionPipe : 'image' }}
69
+ ```
70
+
71
+ ```typescript
72
+ readonly fileDoc: IFile = { name: 'requirements.docx', size: '5 KB' };
73
+ ```
74
+
75
+ ### 4. Ứng dụng: Hiển thị Icon theo loại file
76
+
77
+ ```html
78
+ @for (file of fileList; track file.name) {
79
+ <div class="flex items-center gap-2 mb-2">
80
+ @if (file | LibsUiPipesCheckFileExtensionPipe : 'image') {
81
+ <span>🖼️ Image:</span>
82
+ }
83
+ @if (file | LibsUiPipesCheckFileExtensionPipe : 'video') {
84
+ <span>🎥 Video:</span>
85
+ }
86
+ @if (file | LibsUiPipesCheckFileExtensionPipe : 'word') {
87
+ <span>📝 Word:</span>
88
+ }
89
+ <span>{{ file.name }}</span>
90
+ </div>
91
+ }
92
+ ```
93
+
94
+ ```typescript
95
+ readonly fileList: IFile[] = [
96
+ { name: 'holiday-photo.jpg' },
97
+ { name: 'presentation.mp4' },
98
+ { name: 'requirements.docx' }
99
+ ];
100
+ ```
101
+
102
+ ## API Reference
103
+
104
+ ### LibsUiPipesCheckFileExtensionPipe
105
+
106
+ #### Parameters
107
+
108
+ | Property | Type | Default | Description |
109
+ | -------- | ---------------------------------------------------------------------- | ------- | ------------------------------------------------- |
110
+ | `file` | `IFile \| any` | `-` | Đối tượng file cần kiểm tra (cần có `name`, `mimetype`, hoặc `type`). |
111
+ | `type` | `'image' \| 'video' \| 'word' \| 'xlsx' \| 'pdf' \| 'pptx' \| 'other'` | `-` | Nhóm định dạng muốn kiểm tra. |
112
+
113
+ ## Types & Interfaces
114
+
115
+ ### IFile
116
+
117
+ ```typescript
118
+ export interface IFile {
119
+ id?: string;
120
+ name?: string;
121
+ file?: File;
122
+ size?: string;
123
+ isUploading?: boolean;
124
+ percentUploading?: number;
125
+ isUpdate?: boolean;
126
+ url?: string;
127
+ origin_url?: string;
128
+ mimetype?: string;
129
+ type?: TYPE_FILE;
130
+ error?: string;
131
+ isAvatar?: boolean;
132
+ }
133
+
134
+ export type TYPE_FILE = 'document' | 'image' | 'video' | 'audio';
135
+ ```
136
+
137
+ **Mô tả**: Interface cho đối tượng file. Pipe yêu cầu ít nhất một trong các thuộc tính: `name`, `mimetype`, hoặc `type` (từ `file.type` nếu là File object) để hoạt động.
138
+
139
+ ## Lưu ý quan trọng (Important Notes)
140
+
141
+ > ⚠️ **Caveats**:
142
+ >
143
+ > - **Yêu cầu đối tượng File**: Pipe này yêu cầu input là một object có ít nhất một trong các thuộc tính: `name`, `mimetype`, hoặc `type` (từ `file.type` nếu là File object). Pipe sẽ tự động extract extension từ `name` hoặc sử dụng trực tiếp `mimetype`/`type`. Nếu không tìm thấy, pipe sẽ trả về `false`.
144
+ > - **Hỗ trợ MIME Type**: Đối với các loại văn bản (word, xlsx, pdf...), pipe hỗ trợ kiểm tra cả extension rút gọn (ví dụ: `pdf`) và MIME type đầy đủ (ví dụ: `application/pdf`).
145
+ > - **Phân loại "Other"**: Loại `other` sẽ trả về `true` cho bất kỳ định dạng nào **KHÔNG** nằm trong danh sách mặc định của Image, Video và Documents phổ biến.
146
+ > - **Hiệu năng**: Pipe này là pure logic, có thể sử dụng an toàn trong các danh sách dài mà không gây ảnh hưởng hiệu năng đáng kể.
147
+
148
+ ## Công nghệ sử dụng
149
+
150
+ - Angular 18+
151
+ - TypeScript 5+
152
+
153
+ ## Demo
154
+
155
+ - **Local Development**: [http://localhost:4500/pipes/check-file-extension](http://localhost:4500/pipes/check-file-extension)
156
+
157
+ ## Unit Tests
158
+
159
+ Xem file `test-commands.md` để biết cách chạy unit tests.
160
+
161
+ ## License
162
+
163
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libs-ui/pipes-check-file-extension",
3
- "version": "0.2.355-9",
3
+ "version": "0.2.356-1",
4
4
  "peerDependencies": {
5
5
  "@angular/core": ">=18.0.0"
6
6
  },