Skip to content

Commit 782d1b6

Browse files
committed
feat(kitsu-core): add splitModel
1 parent 046b51a commit 782d1b6

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

packages/kitsu-core/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './filterIncludes'
55
export * from './linkRelationships'
66
export * from './query'
77
export * from './serialise'
8+
export * from './splitModel'
89
export { default as camel } from './camel'
910
export { default as kebab } from './kebab'
1011
export { default as snake } from './snake'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
/**
3+
* splitModel
4+
*
5+
* @param {String} url URL path for the model
6+
* @param {Object} options
7+
* @param {Function} options.resourceCase Convert libraryEntries to library-entries or library_entries (default no conversion). To use parameter, import kebab or snake from kitsu-core
8+
* @param {Function} options.pluralModel Pluralise models (default no pluralisation). To use parameter, import pluralize (or another pluralisation npm package)
9+
* @returns {Array} Array containing the model name and the resource URL with pluralisation applied
10+
*
11+
* @example
12+
* splitModel('posts/1/comments')
13+
* // [ 'comments', 'posts/1/comments' ]
14+
*
15+
* @example <caption>With pluralModel option</caption>
16+
* import plural from 'pluralize'
17+
* splitModel('posts/1/comment', { pluralModel: plural })
18+
* // [ 'comment', 'posts/1/comments' ]
19+
*
20+
* @example <caption>With resourceCase option</caption>
21+
* import { kebab, snake } from 'kitsu-core'
22+
* splitModel('libraryEntries', { resourceCase: kebab })
23+
* // [ 'libraryEntries', 'library-entries' ]
24+
*
25+
* splitModel('libraryEntries', { resourceCase: snake })
26+
* // [ 'libraryEntries', 'library_entries' ]
27+
*/
28+
export function splitModel (url, options = {}) {
29+
if (!options.pluralModel) options.pluralModel = s => s
30+
if (!options.resourceCase) options.resourceCase = s => s
31+
32+
const urlSegments = url.split('/')
33+
const resourceModel = urlSegments.pop()
34+
urlSegments.push(options.pluralModel(options.resourceCase(resourceModel)))
35+
const newUrl = urlSegments.join('/')
36+
37+
return [ resourceModel, newUrl ]
38+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { splitModel } from './'
2+
import { kebab, snake } from '../'
3+
import plural from 'pluralize'
4+
5+
describe('kitsu-core', () => {
6+
describe('splitModel', () => {
7+
it('anime -> anime', () => {
8+
expect.assertions(1)
9+
expect(splitModel('anime'))
10+
.toStrictEqual([ 'anime', 'anime' ])
11+
})
12+
13+
it('anime -> anime (plural, mass noun)', () => {
14+
expect.assertions(1)
15+
expect(splitModel('anime', {
16+
pluralModel: plural
17+
}))
18+
.toStrictEqual([ 'anime', 'anime' ])
19+
})
20+
21+
it('post -> post', () => {
22+
expect.assertions(1)
23+
expect(splitModel('post'))
24+
.toStrictEqual([ 'post', 'post' ])
25+
})
26+
27+
it('post -> posts (plural)', () => {
28+
expect.assertions(1)
29+
expect(splitModel('post', {
30+
pluralModel: plural
31+
}))
32+
.toStrictEqual([ 'post', 'posts' ])
33+
})
34+
35+
it('post/1/relationships/comment -> comment', () => {
36+
expect.assertions(1)
37+
expect(splitModel('post/1/relationships/comment'))
38+
.toStrictEqual([ 'comment', 'post/1/relationships/comment' ])
39+
})
40+
41+
it('post/1/relationships/comment -> comment (plural)', () => {
42+
expect.assertions(1)
43+
expect(splitModel('post/1/relationships/comment', {
44+
pluralModel: plural
45+
}))
46+
.toStrictEqual([ 'comment', 'post/1/relationships/comments' ])
47+
})
48+
49+
it('libraryEntry -> library-entry', () => {
50+
expect.assertions(1)
51+
expect(splitModel('libraryEntry', {
52+
resourceCase: kebab
53+
}))
54+
.toStrictEqual([ 'libraryEntry', 'library-entry' ])
55+
})
56+
57+
it('libraryEntry -> library_entry', () => {
58+
expect.assertions(1)
59+
expect(splitModel('libraryEntry', {
60+
resourceCase: snake
61+
}))
62+
.toStrictEqual([ 'libraryEntry', 'library_entry' ])
63+
})
64+
})
65+
})

0 commit comments

Comments
 (0)