@@ -1094,19 +1094,10 @@ export class OrderVertexTraversal<
10941094 propertyName : TPropertyName ,
10951095 direction : OrderDirection = "asc" ,
10961096 ) {
1097- const steps = [ ...this . steps ] ;
1098- let orderStep =
1099- steps . length > 0 && steps [ steps . length - 1 ] instanceof OrderStep
1100- ? steps [ steps . length - 1 ]
1101- : undefined ;
1102- if ( ! orderStep ) {
1103- steps . push ( new OrderStep ( { directions : [ { key : propertyName , direction } ] } ) ) ;
1104- } else {
1105- steps [ steps . length - 1 ] = orderStep . clone ( {
1106- directions : [ ...orderStep . config . directions , { key : propertyName , direction } ] ,
1107- } ) ;
1108- }
1109- return new OrderVertexTraversal < TSchema , TPath > ( this . graph , steps ) ;
1097+ return new OrderVertexTraversal < TSchema , TPath > (
1098+ this . graph ,
1099+ appendOrderDirection ( this . steps , { key : propertyName , direction } ) ,
1100+ ) ;
11101101 }
11111102}
11121103
@@ -1130,10 +1121,80 @@ export class ValueTraversal<const TSchema extends GraphSchema, const TValue> ext
11301121 new ValuesStep ( { } ) ,
11311122 ] ) ;
11321123 }
1124+
1125+ /**
1126+ * Order the values in the traversal.
1127+ */
1128+ public order ( ) {
1129+ return new OrderValueTraversal < TSchema , TValue > ( this . graph , [
1130+ ...this . steps ,
1131+ new OrderStep ( { directions : [ ] } ) ,
1132+ ] ) ;
1133+ }
1134+ }
1135+
1136+ export class OrderValueTraversal <
1137+ const TSchema extends GraphSchema ,
1138+ const TValue ,
1139+ > extends ValueTraversal < TSchema , TValue > {
1140+ /**
1141+ * Order the values in the traversal by their natural value.
1142+ */
1143+ public by ( ) : OrderValueTraversal < TSchema , TValue > ;
1144+ /**
1145+ * Order the values in the traversal by a property on the value.
1146+ * @param propertyName The name of the property to order by.
1147+ * @param direction The direction to order by.
1148+ */
1149+ public by < const TPropertyName extends keyof GetValueTraversalProperties < TValue > & string > (
1150+ propertyName : TPropertyName ,
1151+ direction ?: OrderDirection ,
1152+ ) : OrderValueTraversal < TSchema , TValue > ;
1153+ public by < const TPropertyName extends keyof GetValueTraversalProperties < TValue > & string > (
1154+ propertyName ?: TPropertyName ,
1155+ direction : OrderDirection = "asc" ,
1156+ ) {
1157+ return new OrderValueTraversal < TSchema , TValue > (
1158+ this . graph ,
1159+ appendOrderDirection ( this . steps , { key : propertyName , direction } ) ,
1160+ ) ;
1161+ }
11331162}
11341163
11351164type ValueOf < T > = T [ keyof T ] ;
11361165
1166+ type GetValueTraversalProperties < TValue > =
1167+ TValue extends Element < any , any , infer TProperties , any >
1168+ ? TProperties
1169+ : TValue extends object
1170+ ? TValue
1171+ : never ;
1172+
1173+ function appendOrderDirection (
1174+ steps : readonly Step < any > [ ] ,
1175+ orderDirection : {
1176+ key ?: string ;
1177+ direction : OrderDirection ;
1178+ } ,
1179+ ) {
1180+ const nextSteps = [ ...steps ] ;
1181+ const orderStep =
1182+ nextSteps . length > 0 && nextSteps [ nextSteps . length - 1 ] instanceof OrderStep
1183+ ? nextSteps [ nextSteps . length - 1 ]
1184+ : undefined ;
1185+
1186+ if ( ! orderStep ) {
1187+ nextSteps . push ( new OrderStep ( { directions : [ orderDirection ] } ) ) ;
1188+ return nextSteps ;
1189+ }
1190+
1191+ nextSteps [ nextSteps . length - 1 ] = orderStep . clone ( {
1192+ directions : [ ...orderStep . config . directions , orderDirection ] ,
1193+ } ) ;
1194+
1195+ return nextSteps ;
1196+ }
1197+
11371198type ResolveTraversalPathProperty <
11381199 TSchema extends GraphSchema ,
11391200 TPath ,
0 commit comments