Skip to content

Commit eb0d510

Browse files
committed
feat: extract thumbhash to separate package
1 parent cf95e2d commit eb0d510

92 files changed

Lines changed: 3076 additions & 565 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bun.lock

Lines changed: 450 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/__tests__/image-loaders.harness.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Platform } from 'react-native'
22
import { describe, expect, it } from 'react-native-harness'
33
import { Images, loadImage } from 'react-native-nitro-image'
4+
import { ThumbHash } from 'react-native-nitro-image-thumbhash'
45
import { WebImages } from 'react-native-nitro-web-image'
56

67
const RED = { r: 1, g: 0, b: 0, a: 1 }
@@ -96,19 +97,19 @@ describe('Images.loadFromFile', () => {
9697
})
9798
})
9899

99-
describe('Images.loadFromThumbHash', () => {
100+
describe('ThumbHash.decode', () => {
100101
it('decodes a ThumbHash buffer back into an Image', () => {
101102
const source = Images.createBlankImage(64, 64, true, BLUE).resize(32, 32)
102-
const hash = source.toThumbHash()
103-
const decoded = Images.loadFromThumbHash(hash)
103+
const hash = ThumbHash.encode(source)
104+
const decoded = ThumbHash.decode(hash)
104105
expect(decoded.width).toBeGreaterThan(0)
105106
expect(decoded.height).toBeGreaterThan(0)
106107
})
107108

108109
it('async variant decodes the ThumbHash buffer', async () => {
109110
const source = Images.createBlankImage(64, 64, true, GREEN).resize(32, 32)
110-
const hash = await source.toThumbHashAsync()
111-
const decoded = await Images.loadFromThumbHashAsync(hash)
111+
const hash = await ThumbHash.encodeAsync(source)
112+
const decoded = await ThumbHash.decodeAsync(hash)
112113
expect(decoded.width).toBeGreaterThan(0)
113114
expect(decoded.height).toBeGreaterThan(0)
114115
})

example/__tests__/image-utils.harness.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import {
33
Images,
44
supportsHeicLoading,
55
supportsHeicWriting,
6-
thumbHashFromBase64String,
7-
thumbHashToBase64String,
86
} from 'react-native-nitro-image'
7+
import { ThumbHash } from 'react-native-nitro-image-thumbhash'
98

109
describe('ImageUtils - HEIC round-trip', () => {
1110
it('encodes an image as HEIC, writes it to disk and loads it back', async () => {
@@ -44,7 +43,7 @@ describe('ImageUtils - HEIC round-trip', () => {
4443
})
4544
})
4645

47-
describe('ImageUtils - thumbHash round-trip', () => {
46+
describe('ThumbHash - round-trip', () => {
4847
it('encodes a small image to a thumbHash and converts to base64', () => {
4948
const image = Images.createBlankImage(64, 64, true, {
5049
r: 0.5,
@@ -53,14 +52,14 @@ describe('ImageUtils - thumbHash round-trip', () => {
5352
a: 1,
5453
})
5554
const small = image.resize(32, 32)
56-
const hash = small.toThumbHash()
55+
const hash = ThumbHash.encode(small)
5756
expect(hash.byteLength).toBeGreaterThan(0)
5857

59-
const base64 = thumbHashToBase64String(hash)
58+
const base64 = ThumbHash.toBase64String(hash)
6059
expect(base64.length).toBeGreaterThan(0)
6160
expect(base64).toMatch(/^[A-Za-z0-9+/=]+$/)
6261

63-
const restored = thumbHashFromBase64String(base64)
62+
const restored = ThumbHash.fromBase64String(base64)
6463
expect(restored.byteLength).toBe(hash.byteLength)
6564
})
6665

@@ -71,8 +70,8 @@ describe('ImageUtils - thumbHash round-trip', () => {
7170
b: 0,
7271
a: 1,
7372
})
74-
const hash = image.toThumbHash()
75-
const decoded = Images.loadFromThumbHash(hash)
73+
const hash = ThumbHash.encode(image)
74+
const decoded = ThumbHash.decode(hash)
7675
expect(decoded.width).toBeGreaterThan(0)
7776
expect(decoded.height).toBeGreaterThan(0)
7877
})

0 commit comments

Comments
 (0)