1- import { Bud } from '@roots/bud-framework'
1+ import type { Bud } from '@roots/bud-framework'
2+
3+ import { InputError } from '@roots/bud-support/errors'
24
35export type Value =
4- | ( ( hash : boolean | undefined ) => boolean )
6+ | ( ( hash ? : boolean ) => boolean | string )
57 | boolean
68 | Bud
79 | string
@@ -12,34 +14,70 @@ export interface hash {
1214 ( value : Value ) : Bud
1315}
1416
15- export const hash : hash = function ( this : Bud , value ) {
16- if ( value instanceof Bud || value === undefined ) {
17- this . context . hash = true
17+ export const hash : hash = function ( this : Bud , value = true ) {
18+ if ( typeof value === `boolean` ) {
19+ return setHash ( this , value )
20+ }
21+
22+ if ( value instanceof this . constructor ) {
23+ return setHash ( this , true )
24+ }
1825
19- this . api . logger . success ( `bud.hash: hash set to` , this . context . hash )
26+ if ( typeof value === `function` ) {
27+ value = value ( this . context . hash )
2028
21- return this
29+ if ( typeof value !== `boolean` && typeof value !== `string` )
30+ throw new InputError ( `bud.hash: invalid input` , {
31+ details : `callbacks supplied to bud.hash should return a boolean or a string value` ,
32+ docs : new URL ( `https://bud.js.org/reference/bud.hash` ) ,
33+ thrownBy : `@roots/bud-api/methods/hash` ,
34+ } )
35+
36+ if ( typeof value === `string` ) {
37+ setHash ( this , true )
38+ setFormat ( this , value )
39+ return this
40+ }
41+
42+ return setHash ( this , value )
2243 }
2344
2445 if ( typeof value === `string` ) {
25- if ( ! value . startsWith ( `[` ) ) value = `[${ value } ]`
46+ setHash ( this , true )
47+ return setFormat ( this , value )
48+ }
2649
27- this . context . hash = true
28- this . hooks . on ( `value.hashFormat` , value )
50+ throw new InputError ( `bud.hash: invalid input` , {
51+ details : `bud.hash accepts a boolean, string, or callback function as input.` ,
52+ docs : new URL ( `https://bud.js.org/reference/bud.hash` ) ,
53+ thrownBy : `@roots/bud-api/methods/hash` ,
54+ } )
55+ }
2956
30- this . api . logger
31- . success ( `bud.hash: hash set to` , this . context . hash )
32- . success (
33- `bud.hash: hash format set to` ,
34- this . hooks . filter ( `value.hashFormat` ) ,
35- )
57+ const formatHashString = ( value : string ) : string => {
58+ if ( ! value . startsWith ( `[` ) ) value = `[${ value } `
59+ if ( ! value . endsWith ( `]` ) ) value = `${ value } ]`
60+ return value
61+ }
3662
37- return this
38- }
63+ const setFormat = ( bud : Bud , value : string ) : Bud => {
64+ value = formatHashString ( value )
65+
66+ bud . hooks
67+ . on ( `value.hashFormat` , value )
68+ . api . logger . success ( `bud.hash: hash format set to` , value )
69+
70+ return bud
71+ }
3972
40- this . context . hash = this . maybeCall ( value , this . context . hash )
73+ const setHash = ( bud : Bud , value : boolean ) : Bud => {
74+ bud . context . hash = value
4175
42- this . api . logger . success ( `bud.hash: hash set to` , this . context . hash )
76+ bud . api . logger . success (
77+ `bud.hash:` ,
78+ `hash` ,
79+ value ? `enabled` : `disabled` ,
80+ )
4381
44- return this
82+ return bud
4583}
0 commit comments