Skip to content

Commit

Permalink
feat: helpers that allows working with mime type and file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
KiraLT committed Jun 10, 2023
1 parent 6ef5976 commit 76fef41
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 40 deletions.
14 changes: 2 additions & 12 deletions spec/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,8 @@ describe('intersection', () => {
})

it('supports empty array', () => {
expect(
intersection([
[],
[2, 3],
])
).toEqual([])
expect(
intersection([
[2, 3],
[],
])
).toEqual([])
expect(intersection([[], [2, 3]])).toEqual([])
expect(intersection([[2, 3], []])).toEqual([])
})

it('supports key callback', () => {
Expand Down
6 changes: 3 additions & 3 deletions spec/encoding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ describe('base64Decode', () => {

describe('base64Decode', () => {
it('decodes string', () => {
expect(base64Decode('cnTEl8SZcmZndDU4xK/ElznEr8SXxaErxJcqLeOBr+S4qg==')).toBe(
'rtėęrfgt58įė9įėš+ė*-は个'
)
expect(
base64Decode('cnTEl8SZcmZndDU4xK/ElznEr8SXxaErxJcqLeOBr+S4qg==')
).toBe('rtėęrfgt58įė9įėš+ė*-は个')
})
})

Expand Down
54 changes: 53 additions & 1 deletion spec/files.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatBytes, getFileParts, parseSize } from '../src'
import { formatBytes, getFileParts, parseSize, getMimeType, getExtension } from '../src'

describe('formatBytes', () => {
it('handles zero', () => {
Expand Down Expand Up @@ -64,3 +64,55 @@ describe('getFileParts', () => {
expect(getFileParts('/my.home/.data')).toEqual(['/my.home/.data', ''])
})
})

describe('getMimeType', () => {
it('returns undefined for unknown extension', () => {
expect(getMimeType('myfile.unknown')).toBeUndefined()
})

it('returns undefined for empty extension', () => {
expect(getMimeType('myfile')).toBeUndefined()
})

it('returns undefined for empty file name', () => {
expect(getMimeType('')).toBeUndefined()
})

it('returns undefined for empty file name and empty extension', () => {
expect(getMimeType('.')).toBeUndefined()
})

it('returns undefined for dot file', () => {
expect(getMimeType('.data')).toBeUndefined()
})

it('returns mime type for text file with extension', () => {
expect(getMimeType('data.txt')).toEqual('text/plain')
})

it('returns mime type for json', () => {
expect(getMimeType('json')).toEqual('application/json')
})
})

describe('getExtension', () => {
it('returns undefined for unknown mime type', () => {
expect(getExtension('application/unknown')).toBeUndefined()
})

it('returns undefined for empty mime type', () => {
expect(getExtension('')).toBeUndefined()
})

it('returns undefined for empty mime type and empty extension', () => {
expect(getExtension('.')).toBeUndefined()
})

it('returns extension for text file with mime type', () => {
expect(getExtension('text/plain')).toEqual('txt')
})

it('returns extension for json', () => {
expect(getExtension('application/json')).toEqual('json')
})
})
4 changes: 2 additions & 2 deletions spec/guards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ describe('isPlainObject', () => {
it('checks if is plain object', () => {
expect(isPlainObject({ a: 1 })).toBeTruthy()
expect(isPlainObject({})).toBeTruthy()
expect(isPlainObject({ a: [1, {}]})).toBeTruthy()
expect(isPlainObject({ a: [1, {}] })).toBeTruthy()
expect(isPlainObject('a')).toBeFalsy()
expect(isPlainObject(1)).toBeFalsy()
expect(isPlainObject(new Boolean(false))).toBeFalsy()
expect(isPlainObject(new Date())).toBeFalsy()
expect(isPlainObject([])).toBeFalsy()
expect(isPlainObject(null)).toBeFalsy()
expect(isPlainObject(undefined)).toBeFalsy()
expect(isPlainObject(new class A {})).toBeFalsy()
expect(isPlainObject(new (class A {})())).toBeFalsy()
})
})

Expand Down
26 changes: 14 additions & 12 deletions spec/object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,21 @@ describe('merge', () => {
})

it('merges arrays', () => {
expect(merge([1, 2], [3, 4], { arrayPolicy: 'merge' })).toEqual([1, 2, 3, 4])
expect(merge([1, 2], [3, 4], { arrayPolicy: 'merge' })).toEqual([
1, 2, 3, 4,
])
})

it('supports custom array merge function', () => {
expect(merge([1, 2], [3, 4], { arrayPolicy: (a, b) => b.concat(a) })).toEqual([3, 4, 1, 2])
expect(
merge([1, 2], [3, 4], { arrayPolicy: (a, b) => b.concat(a) })
).toEqual([3, 4, 1, 2])
})

it('skip nulls', () => {
expect(merge({ a: 1 }, { a: null }, { skipNulls: true })).toEqual({ a: 1 })
expect(merge({ a: 1 }, { a: null }, { skipNulls: true })).toEqual({
a: 1,
})
})
})

Expand Down Expand Up @@ -273,20 +279,16 @@ describe('convertToNested', () => {

describe('getByKey', () => {
it('get value by string key', () => {
expect(
getByKey({ a: { b: [1, {a: 10}]}}, 'a.b.1.a')
).toBe(10)
expect(getByKey({ a: { b: [1, { a: 10 }] } }, 'a.b.1.a')).toBe(10)
})

it('get string value by key path list', () => {
expect(
getByKey({ a: { b: [1, {a: 10}]}}, ['a', 'b', 1, 'a'])
).toBe(10)
expect(getByKey({ a: { b: [1, { a: 10 }] } }, ['a', 'b', 1, 'a'])).toBe(
10
)
})

it('get unknown property', () => {
expect(
getByKey(new Date(), ['a', 'b', 1, 'a'])
).toBeUndefined()
expect(getByKey(new Date(), ['a', 'b', 1, 'a'])).toBeUndefined()
})
})
13 changes: 11 additions & 2 deletions spec/string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { truncate, extractWords, camelCase, pascalCase, titleCase, isLetter } from '../src'
import {
truncate,
extractWords,
camelCase,
pascalCase,
titleCase,
isLetter,
} from '../src'

describe('truncate', () => {
it('truncates string', () => {
Expand Down Expand Up @@ -58,6 +65,8 @@ describe('isLetter', () => {

describe('titleCase', () => {
it('changes string', () => {
expect(titleCase('hello-world FTW,abc999t t')).toEqual('Hello-World Ftw,Abc999T T')
expect(titleCase('hello-world FTW,abc999t t')).toEqual(
'Hello-World Ftw,Abc999T T'
)
})
})

0 comments on commit 76fef41

Please sign in to comment.