-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,73 @@ | ||
package com.kemsky.filters | ||
{ | ||
import com.kemsky.support.toValue; | ||
|
||
/** | ||
* Creates function that extracts named property from value | ||
* @param value item or function applied to item | ||
* @param name name of the property | ||
* @return function function that extracts named property from value | ||
*/ | ||
public function prop(value:*, name:String):Function | ||
public function prop(name:String):Function | ||
{ | ||
return function (item:*):* | ||
var path:Array = name.split("."); | ||
|
||
var p1:String = path.length > 0 ? path[0]: undefined; | ||
var p2:String = path.length > 1 ? path[1]: undefined; | ||
var p3:String = path.length > 2 ? path[2]: undefined; | ||
var p4:String = path.length > 3 ? path[3]: undefined; | ||
var p5:String = path.length > 4 ? path[4]: undefined; | ||
|
||
switch (path.length) | ||
{ | ||
item = toValue(item, value); | ||
return item.hasOwnProperty(name) ? item[name] : undefined; | ||
}; | ||
case 1: | ||
return function (item:*):* | ||
{ | ||
return item.hasOwnProperty(p1) ? item[p1] : undefined; | ||
}; | ||
case 2: | ||
return function (item:*):* | ||
{ | ||
var result:* = undefined; | ||
try | ||
{ | ||
result = item[p1][p2]; | ||
} | ||
catch(e:Error){} | ||
return result; | ||
}; | ||
case 3: | ||
return function (item:*):* | ||
{ | ||
var result:* = undefined; | ||
try | ||
{ | ||
result = item[p1][p2][p3]; | ||
} | ||
catch(e:Error){} | ||
return result; | ||
}; | ||
case 4: | ||
return function (item:*):* | ||
{ | ||
var result:* = undefined; | ||
try | ||
{ | ||
result = item[p1][p2][p3][p4]; | ||
} | ||
catch(e:Error){} | ||
return result; | ||
}; | ||
case 5: | ||
return function (item:*):* | ||
{ | ||
var result:* = undefined; | ||
try | ||
{ | ||
result = item[p1][p2][p3][p4][p5]; | ||
} | ||
catch(e:Error){} | ||
return result; | ||
}; | ||
} | ||
|
||
throw new Error("Property is too deep: '" + name + "', maximum nesting is 5"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters