Skip to content

Commit

Permalink
Add nullable method to type attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Nov 8, 2018
1 parent bc8d01b commit 3628259
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/attributes/types/Boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export default class Boolean extends Type {
*/
value: boolean

/**
* Whether if it can accept `null` as a value.
*/
isNullable: boolean = false

/**
* Create a new number instance.
*/
Expand All @@ -17,6 +22,15 @@ export default class Boolean extends Type {
this.value = value
}

/**
* Set `nullable` to be `true`.
*/
nullable (): this {
this.isNullable = true

return this
}

/**
* Convert given value to the appropriate value for the attribute.
*/
Expand Down Expand Up @@ -50,6 +64,10 @@ export default class Boolean extends Type {
return !!value
}

if (value === null && this.isNullable) {
return value
}

return false
}
}
18 changes: 18 additions & 0 deletions src/attributes/types/Number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export default class Number extends Type {
*/
value: number

/**
* Whether if it can accept `null` as a value.
*/
isNullable: boolean = false

/**
* Create a new number instance.
*/
Expand All @@ -17,6 +22,15 @@ export default class Number extends Type {
this.value = value
}

/**
* Set `nullable` to be `true`.
*/
nullable (): this {
this.isNullable = true

return this
}

/**
* Convert given value to the appropriate value for the attribute.
*/
Expand Down Expand Up @@ -44,6 +58,10 @@ export default class Number extends Type {
return value ? 1 : 0
}

if (value === null && this.isNullable) {
return value
}

return 0
}
}
20 changes: 19 additions & 1 deletion src/attributes/types/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export default class String extends Type {
*/
value: string

/**
* Whether if it can accept `null` as a value.
*/
isNullable: boolean = false

/**
* Create a new string instance.
*/
Expand All @@ -17,6 +22,15 @@ export default class String extends Type {
this.value = value
}

/**
* Set `nullable` to be `true`.
*/
nullable (): this {
this.isNullable = true

return this
}

/**
* Convert given value to the appropriate value for the attribute.
*/
Expand All @@ -27,7 +41,7 @@ export default class String extends Type {
/**
* Convert given value to the string.
*/
fix (value: any): string {
fix (value: any): string | null {
if (value === undefined) {
return this.value
}
Expand All @@ -36,6 +50,10 @@ export default class String extends Type {
return value
}

if (value === null && this.isNullable) {
return value
}

return value + ''
}
}
39 changes: 39 additions & 0 deletions test/feature/attributes/Boolean.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,43 @@ describe('Feature – Attributes – Boolean', () => {
expect(users[6].bool).toEqual(true)
expect(users[7].bool).toEqual(false)
})

it('can accept `null` when there is `nullable` chain', async () => {
class User extends Model {
static entity = 'users'

static fields () {
return {
id: this.attr(null),
bool: this.boolean(true).nullable()
}
}
}

createStore([{ model: User }])

await User.create({
data: [
{ id: 1 },
{ id: 2, bool: '' },
{ id: 3, bool: 'string' },
{ id: 4, bool: '0' },
{ id: 5, bool: 0 },
{ id: 6, bool: 1 },
{ id: 7, bool: true },
{ id: 8, bool: null }
]
})

const users = User.all()

expect(users[0].bool).toEqual(true)
expect(users[1].bool).toEqual(false)
expect(users[2].bool).toEqual(true)
expect(users[3].bool).toEqual(false)
expect(users[4].bool).toEqual(false)
expect(users[5].bool).toEqual(true)
expect(users[6].bool).toEqual(true)
expect(users[7].bool).toEqual(null)
})
})
35 changes: 35 additions & 0 deletions test/feature/attributes/Number.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,39 @@ describe('Feature – Attributes – Number', () => {
expect(users[4].num).toEqual(0)
expect(users[5].num).toEqual(0)
})

it('can accept `null` when there is `nullable` chain', async () => {
class User extends Model {
static entity = 'users'

static fields () {
return {
id: this.attr(null),
num: this.number(0).nullable()
}
}
}

createStore([{ model: User }])

await User.create({
data: [
{ id: 1 },
{ id: 2, num: 1 },
{ id: 3, num: '2' },
{ id: 4, num: true },
{ id: 5, num: false },
{ id: 6, num: null }
]
})

const users = User.all()

expect(users[0].num).toEqual(0)
expect(users[1].num).toEqual(1)
expect(users[2].num).toEqual(2)
expect(users[3].num).toEqual(1)
expect(users[4].num).toEqual(0)
expect(users[5].num).toEqual(null)
})
})
33 changes: 33 additions & 0 deletions test/feature/attributes/String.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,37 @@ describe('Feature – Attributes – String', () => {
expect(users[3].str).toEqual('true')
expect(users[4].str).toEqual('null')
})

it('can accept `null` when there is `nullable` chain', async () => {
class User extends Model {
static entity = 'users'

static fields () {
return {
id: this.attr(null),
str: this.string('default').nullable()
}
}
}

createStore([{ model: User }])

await User.create({
data: [
{ id: 1 },
{ id: 2, str: 'value' },
{ id: 3, str: 1 },
{ id: 4, str: true },
{ id: 5, str: null }
]
})

const users = User.all()

expect(users[0].str).toEqual('default')
expect(users[1].str).toEqual('value')
expect(users[2].str).toEqual('1')
expect(users[3].str).toEqual('true')
expect(users[4].str).toEqual(null)
})
})

0 comments on commit 3628259

Please sign in to comment.