@iconify/json 2.0.0-beta.1 → 2.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.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@iconify/json",
3
3
  "description": "Iconify icons collection in JSON format",
4
4
  "license": "MIT",
5
- "version": "2.0.0-beta.1",
5
+ "version": "2.0.0",
6
6
  "publishConfig": {
7
7
  "tag": "next"
8
8
  },
@@ -13,6 +13,7 @@
13
13
  "url": "git+ssh://git@github.com/iconify/collections-json.git"
14
14
  },
15
15
  "exports": {
16
+ "./*": "./*",
16
17
  ".": {
17
18
  "require": "./dist/index.js",
18
19
  "import": "./dist/index.mjs"
@@ -24,7 +25,7 @@
24
25
  "lib",
25
26
  "collections.json",
26
27
  "collections.md",
27
- "composer.md",
28
+ "composer.json",
28
29
  "readme.md"
29
30
  ],
30
31
  "main": "dist/index.js",
@@ -33,10 +34,12 @@
33
34
  "scripts": {
34
35
  "build": "rimraf dist && tsup src/index.ts --format cjs,esm --dts",
35
36
  "test-esm": "jest --clearCache && cross-env NODE_OPTIONS=--experimental-vm-modules npx jest --config=jest.esm.config.ts",
36
- "test-cjs": "yarn build && jest --clearCache && jest --config=jest.cjs.config.ts",
37
+ "test-cjs": "npm run build && jest --clearCache && jest --config=jest.cjs.config.ts",
37
38
  "test-locate-esm": "jest --clearCache && cross-env NODE_OPTIONS=--experimental-vm-modules npx jest --config=jest.esm.config.ts src/locate.esm.test.ts -i",
38
- "test-locate-cjs": "yarn build && jest --clearCache && jest --config=jest.cjs.config.ts src/locate.cjs.test.ts -i",
39
- "test": "yarn test-esm && yarn test-cjs && yarn test-locate-esm && yarn test-locate-cjs"
39
+ "test-locate-cjs": "npm run build && jest --clearCache && jest --config=jest.cjs.config.ts src/locate.cjs.test.ts -i",
40
+ "test": "npm run test-esm && npm run test-cjs && npm run test-locate-esm && npm run test-locate-cjs",
41
+ "version": "node sync-version",
42
+ "prepublishOnly": "npm run build && npm run version"
40
43
  },
41
44
  "dependencies": {
42
45
  "@iconify/types": "^1.0.9",
package/readme.md CHANGED
@@ -1,20 +1,28 @@
1
- # Iconify icons collection in JSON format
1
+ # Iconify icon sets in JSON format
2
2
 
3
3
  This is collection of SVG icons created by various authors, released under various free licenses. Some collections require attribution when used for commercial purposes. See [collections.md](./collections.md) for list of collections and their licenses.
4
4
 
5
- All SVG icons have been cleaned up. Content has been optimized, colors for monotone icons have been replaced with currentColor, ready to be inserted in HTML. Tools used for creating this collection are available on [Iconify GitHub repository](https://github.com/iconify), more specifically [@iconify/tools](https://github.com/iconify/tools). You can use it to create your own JSON packages from custom icon sets.
5
+ All icons have been normalized:
6
6
 
7
- This library is intended to be used in packages that build components, such as [Iconify Tools](https://github.com/iconify/tools) and [Iconify React](https://github.com/iconify/iconify-react).
7
+ - Cleaned up, removing anything that is not needed to show icon.
8
+ - Colors for monotone icons have been replaced with `currentColor`, making it easy to change icon color by changing text color.
9
+ - Icon content has been optimised to reduce its size.
8
10
 
9
- Repository is updated daily by fully automated script, so it always contains latest icons from various sources.
11
+ Repository is updated by fully automated script, so it always contains latest icons from various sources.
10
12
 
13
+ ## Format and usage
14
+
15
+ Icon sets are stored in `IconifyJSON` format. TypeScript definition is available in `@iconify/types` package. Documentation is [available on Iconify Documentation website](https://docs.iconify.design/types/iconify-json.html).
16
+
17
+ To work with icon sets, use [Iconify Utils](https://docs.iconify.design/tools/utils/). Utils package works in any JavaScript environment: Node.js, Deno, browsers, isolated JavaScript environments. For PHP applications use [Iconify JSON Tools](https://docs.iconify.design/tools/json/).
18
+
19
+ To create icon sets, use [Iconify Tools](https://docs.iconify.design/tools/tools2/). Tools package works in Node.js, it can import icons from various sources, do cleanup, optimisation, export it in `IconifyJSON` format and generate NPM packages.
11
20
 
12
21
  ## How to get this repository
13
22
 
14
23
  Instructions below are for Node.js and PHP projects.
15
24
 
16
-
17
- #### Node.js
25
+ ### Node.js
18
26
 
19
27
  Run this command to add icons to your project:
20
28
 
@@ -24,17 +32,16 @@ npm install --save @iconify/json
24
32
 
25
33
  Icons will be available in node_modules/@iconify/json
26
34
 
27
- To resolve filename for any json file, use this:
35
+ To resolve filename for any json file, use this if you are using CommonJS syntax:
28
36
 
29
37
  ```js
30
- const icons = require('@iconify/json');
38
+ import { locate } from '@iconify/json';
31
39
 
32
- // returns location of fa.json
33
- let fa = icons.locate('fa');
40
+ // returns location of mdi-light.json
41
+ const mdiLightFilename = locate('mdi-light');
34
42
  ```
35
43
 
36
-
37
- #### PHP
44
+ ### PHP
38
45
 
39
46
  Install and initialize Composer project. See documentation at [https://getcomposer.org](https://getcomposer.org)
40
47
 
@@ -60,75 +67,191 @@ If you don't use Composer, clone GitHub repository and add necessary autoload co
60
67
  To resolve filename for any json file, use this:
61
68
 
62
69
  ```php
63
- // returns location of fa.json
64
- $fa = \Iconify\IconsJSON\Finder::locate('fa');
70
+ // Returns location of mdi-light.json
71
+ $mdiLightLocation = \Iconify\IconsJSON\Finder::locate('mdi-light');
65
72
  ```
66
73
 
67
-
68
- ## Format
74
+ ## Data format
69
75
 
70
76
  Icons used by Iconify are in directory json, in Iconify JSON format.
71
77
 
72
- Why JSON instead of SVG? To load images in bulk.
78
+ Why JSON instead of SVG? There are several reasons for that:
73
79
 
74
- If you need individual SVG images, you can generate them using Iconify JSON Tools. See instructions for [PHP version](https://github.com/iconify/json-tools.php) or [Node.js version](https://github.com/iconify/json-tools.js).
80
+ - Easy to store images in bulk.
81
+ - Contains only content of icon without SVG element, making it easy to manipulate content without doing complex parsing. It also makes it easier to create components, such as React icon component, allowing to use framework native SVG element.
82
+ - Data can contain additional content: aliases for icons, icon set information, categories/tags/themes.
75
83
 
84
+ Why not XML?
85
+
86
+ - JSON is much easier to parse without additional tools. All languages support it.
76
87
 
77
88
  Format of json file is very simple:
78
89
 
79
- ```js
90
+ ```json
80
91
  {
81
- "icons": {
82
- "icon-name": {
83
- "body": "svg body",
84
- "width": width,
85
- "height": height
86
- }
87
- },
88
- "aliases": {
89
- "icon-alias": {
90
- "parent": "icon-name"
91
- }
92
- },
93
- "width": default width,
94
- "height": default height
92
+ "prefix": "mdi-light",
93
+ "icons": {
94
+ "icon-name": {
95
+ "body": "<g />",
96
+ "width": 24,
97
+ "height": 24
98
+ }
99
+ },
100
+ "aliases": {
101
+ "icon-alias": {
102
+ "parent": "icon-name"
103
+ }
104
+ }
95
105
  }
96
106
  ```
97
107
 
98
108
  "icons" object contains list of all icons.
99
109
 
100
110
  Each icon has following properties:
101
- * body - icon body
102
- * width - width in pixels
103
- * height - height in pixels
104
- * rotate - rotation. Default = 0. Values: 0 = 0deg, 1 = 90deg, 2 = 180deg, 3 = 270deg
105
- * hFlip - horizontal flip. Boolean value, default = false
106
- * vFlip - vertical flip. Boolean value, default = false
107
- * hidden - If set to true, icon is hidden. That means icon was removed from collection for some reason, but it is kept in JSON file to prevent applications that rely on old icon from breaking
108
111
 
109
- Width or height might be missing. If icon does not have width or height, use default width or height from root object.
110
- rotate, hFlip and vFlip are all optional.
112
+ - body: icon body.
113
+ - left, top: left and top coordinates of viewBox, default is 0.
114
+ - width, height: dimensions of viewBox, default is 16.
115
+ - rotate: rotation. Default = 0. Values: 0 = 0deg, 1 = 90deg, 2 = 180deg, 3 = 270deg.
116
+ - hFlip: horizontal flip. Boolean value, default = false.
117
+ - vFlip: vertical flip. Boolean value, default = false.
118
+ - hidden: if set to true, icon is hidden. That means icon was removed from collection for some reason, but it is kept in JSON file to prevent applications that rely on old icon from breaking.
111
119
 
112
- Optional "aliases" object contains list of aliases for icons. Format is similar to "icons" object, with additional property "parent" that points to parent icon. Any other properties overwrite properties of parent icon.
120
+ Optional "aliases" object contains list of aliases for icons. Format is similar to "icons" object, but without "body" property and with additional property "parent" that points to parent icon. Transformation properties (rotate, hFlip, vFlip) are merged with parent icon's properties. Any other properties overwrite properties of parent icon.
113
121
 
114
- For more information see developer documentation on [https://iconify.design/docs/json-icon-format/](https://iconify.design/docs/json-icon-format/)
122
+ When multiple icons have the same value, it is moved to root object to reduce duplication:
115
123
 
124
+ ```json
125
+ {
126
+ "prefix": "mdi-light",
127
+ "icons": {
128
+ "icon1": {
129
+ "body": "<g />"
130
+ },
131
+ "icon2": {
132
+ "body": "<g />"
133
+ },
134
+ "icon-20": {
135
+ "body": "<g />",
136
+ "width": 20,
137
+ "height": 20
138
+ }
139
+ },
140
+ "width": 24,
141
+ "height": 24
142
+ }
143
+ ```
144
+
145
+ In example above, "icon1" and "icon2" are 24x24, "icon-20" is 20x20.
146
+
147
+ For more information see developer documentation on [https://docs.iconify.design/types/iconify-json.html](https://docs.iconify.design/types/iconify-json.html)
116
148
 
117
149
  ## Extracting individual SVG icons
118
150
 
119
- See JSON tools readme for instructions for [PHP](https://github.com/iconify/json-tools.php) or [Node.js](https://github.com/iconify/json-tools.js).
151
+ For PHP use [Iconify JSON Tools](https://docs.iconify.design/tools/json/).
152
+
153
+ For JavaScript use [Iconify Utils](https://docs.iconify.design/tools/utils/), though Iconify JSON Tools are also available (but deprecated).
154
+
155
+ Example using Iconify Utils (TypeScript):
156
+
157
+ ```ts
158
+ import { promises as fs } from 'fs';
159
+
160
+ // Function to locate JSON file
161
+ import { locate } from '@iconify/json';
162
+
163
+ // Various functions from Iconify Utils
164
+ import { parseIconSet } from '@iconify/utils/lib/icon-set/parse';
165
+ import { iconToSVG } from '@iconify/utils/lib/svg/build';
166
+ import { defaults } from '@iconify/utils/lib/customisations';
167
+
168
+ (async () => {
169
+ // Locate icons
170
+ const filename = locate('mdi');
171
+
172
+ // Load icon set
173
+ const icons = JSON.parse(await fs.readFile(filename, 'utf8'));
174
+
175
+ // Parse all icons
176
+ const exportedSVG: Record<string, string> = Object.create(null);
177
+ parseIconSet(icons, (iconName, iconData) => {
178
+ if (!iconData) {
179
+ // Invalid icon
180
+ console.error(`Error parsing icon ${iconName}`);
181
+ return;
182
+ }
183
+
184
+ // Render icon
185
+ const renderData = iconToSVG(iconData, {
186
+ ...defaults,
187
+ height: 'auto',
188
+ });
189
+
190
+ // Generate attributes for SVG element
191
+ const svgAttributes: Record<string, string> = {
192
+ 'xmlns': 'http://www.w3.org/2000/svg',
193
+ 'xmlns:xlink': 'http://www.w3.org/1999/xlink',
194
+ ...renderData.attributes,
195
+ };
196
+ const svgAttributesStr = Object.keys(svgAttributes)
197
+ .map(
198
+ (attr) =>
199
+ // No need to check attributes for special characters, such as quotes,
200
+ // they cannot contain anything that needs escaping.
201
+ `${attr}="${svgAttributes[attr as keyof typeof svgAttributes]}"`
202
+ )
203
+ .join(' ');
204
+
205
+ // Generate SVG
206
+ const svg = `<svg ${svgAttributesStr}>${renderData.body}</svg>`;
207
+ exportedSVG[iconName] = svg;
208
+ });
209
+
210
+ // Output directory
211
+ const outputDir = 'mdi-export';
212
+ try {
213
+ await fs.mkdir(outputDir, {
214
+ recursive: true,
215
+ });
216
+ } catch (err) {
217
+ //
218
+ }
219
+
220
+ // Save all files
221
+ const filenames = Object.keys(exportedSVG);
222
+ for (let i = 0; i < filenames.length; i++) {
223
+ const filename = filenames[i];
224
+ const svg = exportedSVG[filename];
225
+ await fs.writeFile(outputDir + '/' + filename + '.svg', svg, 'utf8');
226
+ }
227
+ })();
228
+ ```
229
+
230
+ Example using Iconify JSON Tools:
120
231
 
121
232
  ```js
122
233
  const fs = require('fs');
123
- const {SVG, Collection} = require('@iconify/json-tools');
234
+ const { SVG, Collection } = require('@iconify/json-tools');
235
+
236
+ const outputDir = 'mdi-export';
237
+ try {
238
+ fs.mkdirSync(outputDir, {
239
+ recursive: true,
240
+ });
241
+ } catch (err) {
242
+ //
243
+ }
124
244
 
125
- let collection = new Collection();
245
+ const collection = new Collection();
126
246
  collection.loadIconifyCollection('mdi');
127
- collection.listIcons(true).forEach(icon => {
128
- let svg = new SVG(collection.getIconData(icon));
129
- fs.writeFileSync('mdi-' + icon + '.svg', svg.getSVG({
130
- height: 'auto'
131
- }));
247
+ collection.listIcons(true).forEach((icon) => {
248
+ let svg = new SVG(collection.getIconData(icon));
249
+ fs.writeFileSync(
250
+ outputDir + '/' + icon + '.svg',
251
+ svg.getSVG({
252
+ height: 'auto',
253
+ })
254
+ );
132
255
  });
133
256
  ```
134
257
 
@@ -136,17 +259,19 @@ collection.listIcons(true).forEach(icon => {
136
259
  use \Iconify\JSONTools\Collection;
137
260
  use \Iconify\JSONTools\SVG;
138
261
 
262
+ $outputDir = 'mdi-export';
263
+ @mkdir($outputDir, 0777, true);
264
+
139
265
  $collection = new Collection();
140
266
  $collection->loadIconifyCollection('mdi');
141
267
  foreach ($collection->listIcons(true) as $icon) {
142
268
  $svg = new SVG($collection->getIconData($icon));
143
- file_put_contents('mdi-' . $icon . '.svg', $svg->getSVG([
269
+ file_put_contents($outputDir . '/' . $icon . '.svg', $svg->getSVG([
144
270
  'height' => 'auto'
145
271
  ]));
146
272
  }
147
273
  ```
148
274
 
149
-
150
275
  ## License
151
276
 
152
277
  This is collection of works by various authors, not original collection.