Skip to content

Commit fd75773

Browse files
author
Raymond Ottun
committed
fix: changed svg generator
1 parent c080743 commit fd75773

File tree

8 files changed

+458
-141
lines changed

8 files changed

+458
-141
lines changed

Diff for: package-lock.json

+400-51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
"license": "ISC",
3131
"dependencies": {
3232
"@faker-js/faker": "^8.0.2",
33-
"geopattern": "^1.2.3",
3433
"handlebars": "^4.7.7",
3534
"object-path": "^0.11.8",
35+
"trianglify": "^4.1.1",
3636
"yargs": "^17.7.2"
3737
},
3838
"devDependencies": {

Diff for: src/cmds/generate.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ export const generate = (
4040
): void => {
4141
const { template, output, locale } = argv
4242
try {
43-
44-
if(!fs.existsSync(template)) {
43+
if (!fs.existsSync(template)) {
4544
throw new Error(`File or directory "${template}" does not exist`)
4645
}
4746

Diff for: src/helpers/compare.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Handlebars.registerHelper(
4242

4343
if (!operators[operator]) {
4444
throw new Error(
45-
`Handlerbars Helper "compare" doesn't know the operator ${String(operator)}`
45+
`Handlebars Helper "compare" doesn't know the operator ${String(operator)}`
4646
)
4747
}
4848

Diff for: src/helpers/image.test.ts

+26-46
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,41 @@
11
import Handlebars from 'handlebars'
2-
import Geopattern from 'geopattern'
2+
import trianglify from 'trianglify'
33
import './image'
44

5-
const toDataUrl = jest.fn(() => 'url_image')
6-
const toDataUri = jest.fn(() => 'uri_image')
75
const toSvg = jest.fn(() => 'svg_image')
8-
const toBase64 = jest.fn(() => 'base64_image')
96

10-
jest.mock('geopattern', () => {
11-
return {
12-
generate: jest.fn(() => {
13-
return {
14-
toDataUrl,
15-
toDataUri,
16-
toSvg,
17-
toBase64
18-
}
19-
})
20-
}
7+
jest.mock('trianglify', () => {
8+
return jest.fn(() => {
9+
return {
10+
toSVGTree: toSvg
11+
}
12+
})
2113
})
2214

2315
test('imageURL', () => {
2416
const result = Handlebars.compile(`{{imageURL "boy"}}`)({})
25-
expect(result).toMatchInlineSnapshot(`"url_image"`)
26-
expect(Geopattern.generate).toHaveBeenLastCalledWith('boy', {
27-
baseColor: '#933c3c'
28-
})
29-
})
30-
31-
test('imageURL custom color', () => {
32-
const result = Handlebars.compile(`{{imageURL "boy" "#00000"}}`)({})
33-
expect(result).toMatchInlineSnapshot(`"url_image"`)
34-
expect(Geopattern.generate).toHaveBeenLastCalledWith('boy', {
35-
baseColor: '#00000'
17+
expect(result).toMatchInlineSnapshot(
18+
`"url("data:image/svg+xml;base64,c3ZnX2ltYWdl")"`
19+
)
20+
expect(trianglify).toHaveBeenLastCalledWith({
21+
cellSize: 125,
22+
height: 800,
23+
seed: 'boy',
24+
width: 1200,
25+
yColors: 'match'
3626
})
3727
})
3828

3929
test('imageURI', () => {
40-
const result = Handlebars.compile(`{{imageURI "girl"}}`)({})
41-
expect(result).toMatchInlineSnapshot(`"uri_image"`)
42-
expect(Geopattern.generate).toHaveBeenLastCalledWith('girl', {
43-
baseColor: '#933c3c'
44-
})
45-
})
46-
47-
test('imageSVG', () => {
48-
const result = Handlebars.compile(`{{imageSVG "man"}}`)({})
49-
expect(result).toMatchInlineSnapshot(`"svg_image"`)
50-
expect(Geopattern.generate).toHaveBeenLastCalledWith('man', {
51-
baseColor: '#933c3c'
52-
})
53-
})
54-
55-
test('imageBase64', () => {
56-
const result = Handlebars.compile(`{{imageBase64 "woman"}}`)({})
57-
expect(result).toMatchInlineSnapshot(`"base64_image"`)
58-
expect(Geopattern.generate).toHaveBeenLastCalledWith('woman', {
59-
baseColor: '#933c3c'
30+
const result = Handlebars.compile(`{{imageURI "man"}}`)({})
31+
expect(result).toMatchInlineSnapshot(
32+
`"data:image/svg+xml;base64,c3ZnX2ltYWdl"`
33+
)
34+
expect(trianglify).toHaveBeenLastCalledWith({
35+
cellSize: 125,
36+
height: 800,
37+
seed: 'man',
38+
width: 1200,
39+
yColors: 'match'
6040
})
6141
})

Diff for: src/helpers/image.ts

+14-25
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
import Handlebars from 'handlebars'
2-
import GeoPattern from 'geopattern'
2+
import trianglify from 'trianglify'
33

4-
const DEFAULT_COLOR = '#933c3c'
4+
const generateSVG = ({ seed }: { seed: string }): string => {
5+
const svg = trianglify({
6+
width: 1200,
7+
height: 800,
8+
cellSize: 125,
9+
seed,
10+
yColors: 'match'
11+
}).toSVGTree().toString()
12+
13+
return Buffer.from(svg).toString('base64')
14+
}
515

616
/**
717
* Generate a pattern image from a string.
818
*/
919
Handlebars.registerHelper('imageURL', function (text: string, ...args: any[]) {
10-
const [, baseColor = DEFAULT_COLOR] = args.concat().reverse()
11-
return GeoPattern.generate(text, { baseColor }).toDataUrl()
20+
return `url("data:image/svg+xml;base64,${generateSVG({ seed: text })}")`
1221
})
1322

1423
/**
1524
* Generate a pattern image from a string.
1625
*/
1726
Handlebars.registerHelper('imageURI', function (text: string, ...args: any[]) {
18-
const [, baseColor = DEFAULT_COLOR] = args.concat().reverse()
19-
return GeoPattern.generate(text, { baseColor }).toDataUri()
20-
})
21-
22-
/**
23-
* Generate a pattern image from a string.
24-
*/
25-
Handlebars.registerHelper('imageSVG', function (text: string, ...args: any[]) {
26-
const [, baseColor = DEFAULT_COLOR] = args.concat().reverse()
27-
return GeoPattern.generate(text, { baseColor }).toSvg()
27+
return `data:image/svg+xml;base64,${generateSVG({ seed: text })}`
2828
})
29-
30-
/**
31-
* Generate a pattern image from a string.
32-
*/
33-
Handlebars.registerHelper(
34-
'imageBase64',
35-
function (text: string, ...args: any[]) {
36-
const [, baseColor = DEFAULT_COLOR] = args.concat().reverse()
37-
return GeoPattern.generate(text, { baseColor }).toBase64()
38-
}
39-
)

Diff for: src/helpers/nl.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import Handlebars from 'handlebars'
44
* Replace newlines with <br> tags.
55
*/
66
Handlebars.registerHelper('nlbr', function (text: string) {
7-
// Replace newlines with escape characters
8-
var escapedText = text.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '\\n');
9-
// Surround the text with double quotes
10-
var jsonString = '"' + escapedText + '"';
11-
return jsonString;
7+
// Replace newlines with escape characters
8+
const escapedText = text.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '\\n')
9+
// Surround the text with double quotes
10+
const jsonString = '"' + escapedText + '"'
11+
return jsonString
1212
})

Diff for: src/helpers/select.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import Handlebars from "handlebars";
1+
import Handlebars from 'handlebars'
22

33
/**
44
* Select the first non-empty value from a list of values.
55
*/
6-
Handlebars.registerHelper("select", function (...args: any[]) {
7-
const [, ...rest] = args.concat().reverse()
6+
Handlebars.registerHelper('select', function (...args: any[]) {
7+
const [, ...rest] = args.concat().reverse()
88

9-
return rest.reverse().find((v: any) => {
10-
if(v === undefined || v === null || v === "") {
11-
return false
12-
}
9+
return rest.reverse().find((v: any) => {
10+
if (v === undefined || v === null || v === '') {
11+
return false
12+
}
1313

14-
return true
15-
})
16-
})
14+
return true
15+
})
16+
})

0 commit comments

Comments
 (0)