/src/mochilists.erl
Erlang | 122 lines | 67 code | 15 blank | 40 comment | 0 complexity | 1cb14eab14a7253272827c7d36f449ff MD5 | raw file
1%% @copyright Copyright (c) 2010 Mochi Media, Inc. 2%% @author David Reid <dreid@mochimedia.com> 3%% 4%% Permission is hereby granted, free of charge, to any person obtaining a 5%% copy of this software and associated documentation files (the "Software"), 6%% to deal in the Software without restriction, including without limitation 7%% the rights to use, copy, modify, merge, publish, distribute, sublicense, 8%% and/or sell copies of the Software, and to permit persons to whom the 9%% Software is furnished to do so, subject to the following conditions: 10%% 11%% The above copyright notice and this permission notice shall be included in 12%% all copies or substantial portions of the Software. 13%% 14%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17%% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20%% DEALINGS IN THE SOFTWARE. 21 22%% @doc Utility functions for dealing with proplists. 23 24-module(mochilists). 25-author("David Reid <dreid@mochimedia.com>"). 26-export([get_value/2, get_value/3, is_defined/2, set_default/2, set_defaults/2]). 27 28%% @spec set_default({Key::term(), Value::term()}, Proplist::list()) -> list() 29%% 30%% @doc Return new Proplist with {Key, Value} set if not is_defined(Key, Proplist). 31set_default({Key, Value}, Proplist) -> 32 case is_defined(Key, Proplist) of 33 true -> 34 Proplist; 35 false -> 36 [{Key, Value} | Proplist] 37 end. 38 39%% @spec set_defaults([{Key::term(), Value::term()}], Proplist::list()) -> list() 40%% 41%% @doc Return new Proplist with {Key, Value} set if not is_defined(Key, Proplist). 42set_defaults(DefaultProps, Proplist) -> 43 lists:foldl(fun set_default/2, Proplist, DefaultProps). 44 45 46%% @spec is_defined(Key::term(), Proplist::list()) -> bool() 47%% 48%% @doc Returns true if Propist contains at least one entry associated 49%% with Key, otherwise false is returned. 50is_defined(Key, Proplist) -> 51 lists:keyfind(Key, 1, Proplist) =/= false. 52 53 54%% @spec get_value(Key::term(), Proplist::list()) -> term() | undefined 55%% 56%% @doc Return the value of <code>Key</code> or undefined 57get_value(Key, Proplist) -> 58 get_value(Key, Proplist, undefined). 59 60%% @spec get_value(Key::term(), Proplist::list(), Default::term()) -> term() 61%% 62%% @doc Return the value of <code>Key</code> or <code>Default</code> 63get_value(_Key, [], Default) -> 64 Default; 65get_value(Key, Proplist, Default) -> 66 case lists:keyfind(Key, 1, Proplist) of 67 false -> 68 Default; 69 {Key, Value} -> 70 Value 71 end. 72 73%% 74%% Tests 75%% 76-ifdef(TEST). 77-include_lib("eunit/include/eunit.hrl"). 78 79set_defaults_test() -> 80 ?assertEqual( 81 [{k, v}], 82 set_defaults([{k, v}], [])), 83 ?assertEqual( 84 [{k, v}], 85 set_defaults([{k, vee}], [{k, v}])), 86 ?assertEqual( 87 lists:sort([{kay, vee}, {k, v}]), 88 lists:sort(set_defaults([{k, vee}, {kay, vee}], [{k, v}]))), 89 ok. 90 91set_default_test() -> 92 ?assertEqual( 93 [{k, v}], 94 set_default({k, v}, [])), 95 ?assertEqual( 96 [{k, v}], 97 set_default({k, vee}, [{k, v}])), 98 ok. 99 100get_value_test() -> 101 ?assertEqual( 102 undefined, 103 get_value(foo, [])), 104 ?assertEqual( 105 undefined, 106 get_value(foo, [{bar, baz}])), 107 ?assertEqual( 108 bar, 109 get_value(foo, [{foo, bar}])), 110 ?assertEqual( 111 default, 112 get_value(foo, [], default)), 113 ?assertEqual( 114 default, 115 get_value(foo, [{bar, baz}], default)), 116 ?assertEqual( 117 bar, 118 get_value(foo, [{foo, bar}], default)), 119 ok. 120 121-endif. 122