needier
Dependency ordering with depth first search.
Install
npm install needier --save
Usage
needs = require'needier' # one-step: require and call function needsadd id:'A'needs: 'B''C' # A needs both B and C. B+C added as strings needsadd id:'B'needs:'D'# B needs D. Replaces string B with object needsadd id:'C'needs:'E'# C needs E. Replaces string C with object result = needsadd id:'D'needs:'C'# D needs C. Replaces string D with object # result.added = [ {id:'D', needs:['C']} ] # all functions can accept IDs as strings or as an 'id' property on an object # all functions can accept multiple arguments, or an array of arguments result = needshas 'B''E'result = needshas 'B''E'# same as line above result = needshas id:'B'id:'E' # same as well #result.has = { B:true, E:true } result = needsof 'A' # get list of needs for 'A' #result.needsOf = A:['B', 'C' ] result = needsa 'B' # list what needs 'B' #result.needsA = B:['A'] consolelog needsorderedarray # order the needs and get array from results # prints: [ 'E', 'C', 'D', 'B', 'A' ] # full ordered() results are: # { success:true, had:'needs', array: [seen above] } result = needsremove 'C'#result.removed = [ {id:'C', needs:['E']} ]
API of Needs
needs.add(object+)
need1 = 'someStringId' # a string, will be both id and object need2 = id:'ObjectId1' # an object with `id` property need3 = id:'ObjectId2'needs: 'anotherStringId' # object with id and needs results = needsadd need1need2need3results = # contents are: success:true added: someStringId:'someStringId' # string ObjectID1: id:'ObjectID1' # full object ObjectID2: id:'ObjectID2'needs:'anotherStringId' props anotherStringId: 'anotherStringId' # implicitly added by ObjectID2
needs.include(id, needs+)
Add needs (dependencies) to an existing need.
needsadd id:'A'needs:'B' # three equivalent calls # id can be a string or an object with an id property # needs can be a list of arguments or an array needsinclude 'A''C'needsinclude id:'A''C'needsinclude 'A''C' result = needsof 'A'results = # contents are: success:true needsOf: A:'B''C'
needs.has(id+)
Check existence of need(s) with the specified ids.
# assume it has needs A and B result = needshas 'A''B''C'needshas id:'A'id:'B'id:'C' # same as above needshas 'A''B''C'# same as above result = # results of above calls: success:true has: A:true B:true C:false # access results like: if resulthasA then 'good'
needs.remove(id+)
Remove needs (dependencies). An id
can be a string or an object with an id
property.
needsadd 'A'id:'B'id:'C'needs:'D'# now contains A,B,C,D result = needsremove 'A' needsremove id:'A' # same needsremove id:'A' # same # now contains B,C,D result = # contents are: success:true removed: A:'A' result = needsremove 'C'# now contains B,D. # NOTE: D remains, added implicitly, remove explicitly. TODO: fix this result = # contents are: success:true removed: C:id:'C'needs:'D' result = needsremove 'B''D'needsremove 'B''D'#all alternates allowed # now empty result = # contents are: success:true removed: B:id:'B' D:'D'
needs.retract(id, needs+)
Removes needs (dependencies) from an existing need.
needsadd id:'A'needs:'B''C' # three equivalent calls # id can be a string or an object with an id property # needs can be a list of arguments or an array needsretract 'A''C'needsretract 'A''C'needsretract id:'A''C' result = needsof 'A'results = # contents are: success:true needsOf: A:'B'
needs.replace(id, needs+)
Replaces existing needs (dependencies) of an existing need.
needsadd id:'A'needs:'B''C' # three equivalent calls # id can be a string or an object with an id property # needs can be a list of arguments or an array needsreplace 'A''D''E'needsreplace id:'A''D''E'needsreplace 'A''D''E' result = needsof 'A'results = # contents are: success:true needsOf: A:'D''E'
needs.of(id+)
Gather needs (dependencies) for all specified id's. An id
can be a string or an
object with an id
property.
# all four lines are equivalent needsof 'A''B'needsof 'A''B'needsof id:'A'id:'B'needsof id:'A'id:'B' result = # result of all the above calls are the same: success:true needsOf: # we didn't add any needs (deps), so the arrays are empty A: B:
needs.a(id+)
Gather needs which need (depend on) the specified ids.
needsadd id:'A'needs:'C'id:'B'needs:'C'result = needsa 'C'result = # contents are: success:true needsA: # maps specified need IDs to the objects which needed them 'C': id:'A'needs:'C'id:'B'needs:'C'
needs.ordered()
Provides all known needs ordered into an array.
Note: This function is the primary reason for this library.
needsadd id:'C'needs:'B' # can all be passed to add() at once, too needsadd id:'D'needs:'C'needsadd id:'B'needs:'A'result = needsorderedresult = # contents are: success:true ordered: # basically: A, B, C, D. A is a string, added implicityly by B. 'A'id:'B'needs:'A'id:'C'needs:'B'id:'D'needs:'C'
Todo
- add tests for
get
- add before/after constraints instead of
needs
(which is essentially anafter
constraint) - add tests for before/after
- add tests for include/retract/replace