Commit: a7387e60b18c5f6e27adfd3b96c0b284b377aba6
Parent: 3794605c3e2c8f3fbe54f4bc632b5b652cd62713
Author: Ren Tatsumoto
Date: Thu, 17 Dec 2020 19:02:31 +0300
rewrite join_media_fields() in terms of table.get()
Diffstat:
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/subs2srs.lua b/subs2srs.lua
@@ -99,6 +99,19 @@ function table.max_num(table)
return max
end
+---Returns a value for the given key. If key is not available then returns default value 'nil'.
+---@param table table
+---@param key string
+---@param default any
+---@return any
+function table.get(table, key, default)
+ if table[key] == nil then
+ return default or 'nil'
+ else
+ return table[key]
+ end
+end
+
local function is_empty(var)
return var == nil or var == '' or (type(var) == 'table' and next(var) == nil)
end
@@ -333,6 +346,13 @@ local function export_to_anki(gui)
subs.clear()
end
+local function join_media_fields(new_data, stored_data)
+ for _, field in pairs { config.audio_field, config.image_field } do
+ new_data[field] = table.get(stored_data, field, "") .. table.get(new_data, field, "")
+ end
+ return new_data
+end
+
local function update_last_note(overwrite)
local sub = subs.get()
local last_note_id = ankiconnect.get_last_note_id()
@@ -359,18 +379,6 @@ local function update_last_note(overwrite)
subs.clear()
end
-local function join_media_fields(note1, note2)
- if note2[config.audio_field] then
- note1[config.audio_field] = note2[config.audio_field] .. (note1[config.audio_field] == nil and "" or note1[config.audio_field])
- end
-
- if note2[config.image_field] then
- note1[config.image_field] = note2[config.image_field] .. (note1[config.image_field] == nil and "" or note1[config.image_field])
- end
-
- return note1
-end
-
local validate_config
do
local function is_webp_supported()