Skip to content

Commit

Permalink
fix: 🐛 issue with regular expression for base64 in image shape
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaiklor committed Apr 8, 2020
1 parent 872d3cf commit c3f9be2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/kittik-shape-image/spec/Image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('image shape', () => {
expect(shape.image).toHaveLength(24180);

shape.image = 'non-existing-file.png';
expect(() => shape.image).toThrow('Image is not in base64 or does not exists on file system');
expect(() => shape.image).toThrow('Image is not in base64 or does not exist on file system');
});

it('should properly render the shape', () => {
Expand Down Expand Up @@ -109,6 +109,7 @@ describe('image shape', () => {

it('should properly check if string isBase64', () => {
expect.hasAssertions();

expect(Image.isBase64('dGVzdA==')).toBe(true);
expect(Image.isBase64('not base64')).toBe(false);
});
Expand Down
14 changes: 7 additions & 7 deletions packages/kittik-shape-image/src/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ export class Image extends Shape implements ImageOptions, ShapeRenderable {
}

public static isBase64 (string: string): boolean {
return (/^[A-Za-z0-9+/]{4}*[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==$/u).test(string);
return (/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u).test(string);
}

public get image (): string {
if (Image.isBase64(this._image)) {
return this._image;
} else if (fs.existsSync(path.resolve(process.cwd(), this._image))) {
}

if (fs.existsSync(path.resolve(process.cwd(), this._image))) {
return fs.readFileSync(path.resolve(process.cwd(), this._image), 'base64');
}
throw new Error('Image is not in base64 or does not exists on file system');

throw new Error('Image is not in base64 or does not exist on file system');
}

public set image (image: string) {
Expand All @@ -52,12 +55,9 @@ export class Image extends Shape implements ImageOptions, ShapeRenderable {
public render <T extends Canvas>(cursor: T): void {
super.render(cursor);

const { width } = this;
const { height } = this;
const { width, height, image, preserveAspectRatio } = this;
const x = parseInt(this.x, 10);
const y = parseInt(this.y, 10);
const { image } = this;
const { preserveAspectRatio } = this;
const size = 3 * (image.length / 4);
const args = [
`size=${size}`,
Expand Down

0 comments on commit c3f9be2

Please sign in to comment.